Inheritance - forms in Java - Learners Pool

Learners Pool

Knowledge that matters

Inheritance - forms in Java

Share This

Inheritance and Forms

Inheritance in java is one of the core concepts of Object Oriented Programming. Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends keyword.

Inheritance in java is the method to create a hierarchy between classes by inheriting from other classes. Inheritance in java is transitive – so if Sedan extends Car and Car extends Vehicle, then Sedan is also inherited from Vehicle class. Vehicle becomes the superclass of both Car and Sedan.
In java, every class implicitly extends java.lang.Object class, so Object class is at the top level of inheritance hierarchy in java.

Below are the different types of inheritance which is supported by Java.
1.     Single Inheritance
2.     Multiple Inheritance (Through Interface)
3.     Multilevel Inheritance
4.     Hierarchical Inheritance

5.     Hybrid Inheritance (Through Interface)

1.   Single Inheritance in Java: Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we  call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.

2.   Multiple Inheritance in Java: Multiple Inheritance is nothing but one class extending more than one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces.
3.   Multilevel Inheritance in Java: In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.

4.   Hierarchical Inheritance in Java: In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent class for ClassB, ClassC and ClassD.

5.   Hybrid Inheritance in Java: Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting as Parent for ClassD.

No comments:

Post a Comment