Inheritance In Java
Inheritance in real world example that, a child inherits all the properties of the his parents.
In java Inheritance means a class is inherits all the properties of another class.
The class which gives all of his methods and data members is known as super class or parent class.
The class which takes all the data members and methods from the parent class, is known as child class or derived class or sub class.
A child class have ability to access all the properties of the super class or parent class.
The following diagram shows the real world example of the inheritance.

In the above image there is lion and a cub.
The cub inherits all the properties of the lion.
Inheritance in java is done by extending a sub class/ child class with parent class or super class.
Inheritance in general is class ‘is a’ relationship between a sub class and a parent class.
The example shown below is a sub class extending the super class by using extends keyword.

The above example shows that there a DOG class which is the parent class and there is another class ANIMAL known as sub class/ child class.
Here is the simple code of inheritance java:

The above example is called single inheritance. In which a sub class extends a super class by extending it. The properties of Parent can be used by Child class.
Types Of Inheritance;
There are Many types of inheritance in java programming language;
- Single Inheritance;
In single inheritance there is one super class and one child class. The Child class inherits the super class. The Example of single inheritance is shown above.
2.Multilevel Inheritance;
Multilevel inheritance in another way of obtaining the inheritance.
In this type of inheritance when a class is inherited by another class and so on, this type of inheritance is called Multilevel inheritance.
In this type of inheritance a class have more than one parent classes.

The Example of inheritance id given bellow;

In the above example it is clearly seen that there are more than one class and there are more than one parent class of the Puppy subclass, which is directly sub class of the Dog and indirectly it is sub class of the Animal class.
In this example there are more than one child class of the Animal class as seen in the above example.
3. Hierarchical Inheritance;
Hierarchical inheritance is another way of obtaining the inheritance.
In this type of inheritance one more than one classes are derived from a single base class.
There are more than one subclass of a parent class.
Hierarchical inheritance is shown as;

The example of the hierarchical inheritance is given below;

The above example shows the hierarchical inheritance.
In the above example it is clearly seen that there are more than one sub classes of the Animal class.
There are more than class are derived from the Animal class.
There many more types of the inheritance if you want to learn more about the inheritance and its types than click here;
Multiple Inheritance in java;
In java for reducing the complexity in our java code, Java not support the multiple inheritance.
For understanding the multiple inheritance let us consider the following example scenario;
Let there are three class X, Y and Z.
Let Z inherits both of the class and both of class X and Y.
If X and Y both having the same methods than compiler is become little bit confuse in which method you wants t call??
Than there becomes a compile time error after running the code;
The code of Multiple inheritance is given by;

In the above example of multiple inheritance there appears a compile time error.
Advantages Of Inheritance;
- One of the benefit of using inheritance is the using the same code all over the program.
- Code re usability can be achieved by inheritance which makes the program more efficient.
- Method overriding can be achieved due to the inheritance.
