Category Archives for Concepts

Types of Inheritance in Java and OOPS with code example and Diagram

Inheritance can take different forms in OOPS. Not all variations of inheritance is possible in Java, as Java by design has tried to keep it simple. Here we will discuss types of inheritance in Java with code examples. We will first discuss all the types then make a note where Java does or does not support.

Different types of inheritance

I have covered Java inheritance with code example before. For an over all oops concept you can read Object Oriented Concepts as a story. Here we are going to focus more about different variations of inheritance.

The diagram here depicts types of inheritance and those are supported by Java.

types of inheritance in Java

1. Single Inheritance

Here the inheritance is simple. A Child inherits directly from a Parent.

Code Example:



2. Multiple Inheritance

In case of multiple inheritance a child will have more than one parent. This type of inheritance is not supported in Java

Code Sample:

Since Java do not support no code sample ūüôā


3. Multi-Level Inheritance

In multi-level inheritance, a child will inherit from parent, then the child will act as a parent for other child.

Code Sample:


4. Hierarchical Inheritance

More than a child could be derived from a parent. This is hierarchical inheritance

Code Sample:

5. Hybrid Inheritance

When there is mix of all the above sorts of inheritance is there, it is a hybrid inheritance. Java won’t support anything that involved multiple inheritance.

I hope you enjoyed this post.

Inheritance in Java with code example

Inheritance is an unavoidable concept in OOPS. Let us explore inheritance in conceptual and code level. I will also explain inheritance in Java with practical examples.

So let’s get started:

What is inheritance in Java

Inheritance in Java is to create a class by acquiring methods and properties from another class. A parent-child relationship will be formed. The parent class also could be called as base class or super class. Inherited class will be also known as derived class or child class.

Inheritance is also known as Generalization and Specialization

Why Inheritance

Object Orientation tries to model a system using to real world concepts. Inheritance exists in so many places in the nature. A child can inherit many qualities of a parent.

When it comes to programming, inheritance is a way to acquire methods and properties defined in the super class. This not only makes modelling of a system easy, but makes code reuse much efficient. Child class will get the codes defined for the parent.

There are different types of inheritance that is possible. Java supports the one without multiple inheritance.

Natural Examples of Inheritance

In the nature any child born will have some of the features of both the parents inherited. You may want to refer to my story example for such easy details of inheritance.

Code Example

Assume you are developing an online banking system. You can have many kinds of people using that system. Examples are :

  1. A generic user that casually browses the system to simply see what is it
  2. A user who go registered and became a customer
  3. A Manager that uses the system and maintains other users and resources.

If you look at it, all these are users and they have many things in general. At the same time some users are special. This is where inheritance comes to help. Now you see why inheritance is also known as Generalization and Specialization.

In this example every user will have the following properties

  1. User ID
  2. Name
  3. Age

A registered user can have additional properties such as

  1. Account Status

A manager can have the following property

  1. Number of people reporting to the manager

There is no need to repeat a User’s common properties in registered user and manager. We just need to inherit from the User. That is exactly how inheritance is used in Java.

Diagrammatic Representation of the above Inheritance example java inheritance example

Extends Keyword

In Java inheritance is done using extends keyword. The child class will extend from the parent class.

Is-A relation

While inheriting you can say Child is a type of Parent. Using instanceof operator you can test this out. However, Parent is not a type of child. ie, customerObject instance of User will return true, but userObject instanceof Customer will return false.

Code Example for Java Inheritance

Multiple Inheritance

Java can do many variations of inheritance. However, multiple inheritance is not supported.

Multiple inheritance is when a class can be a child of multiple parents. It is more natural for a child class to have more than one parent. However, when it comes to programming additional complexity will arise. If you inherit from multiple parents properties from both parents will be derived to the child. If both the parents have the property with the same name, it will become complicated to know which property the child is using.

For the sake of simplicity Java does not allow multiple inheritance.

Parent method access with super

When a property or method is inherited by the child class, there is no need to worry if that is defined in the parent or child class. You can use as it is defined in the child class. But there are cases when child will have it in exactly same name. In that case, you can refer to the parent property/method using super keyword.

Inheritance of property and method

In inheritance Child could be treated as a type of Parent. It means, you can create a child object and assign to the parent reference directly. However, when you call the function on this object, the Child’s method will run. But this is not the case for properties. If you access property of with a Parent reference, you will get parent’s property, as opposed to the method. This is explained in the code example.


With the given constructs there are variations of inheritance, such as single and hierarchical inheritance is possible in Java.

So that’s it about Java inheritance. Let me know if you have any questions.



Java Random Number Generator

random_number_generationWe will cover Java Random Number Generator here. When it comes to programming, what you are generating is not really random. What we will have is pseudo randomness. So in essence you cannot exactly mimic the way the dice is rolled. But for practical purposes, these numbers will be random.


Here are different needs to create random numbers in Java Continue reading

Java Class and Object understanding with examples

Java is an object oriented language. So understanding of objects and classes are essentials to the understanding of the language itself. Object orientation is not limited to Java, once you know this it stays the same for all the object oriented languages. In this post, we will cover Java Class and Object concepts with some examples.

So let us  start:

Continue reading

1 2 3