What is Inheritance and what are different types of it?
Inheritance is a mechanism by which a specific object acquires attributes and behaviors of more general objects.In OOP terminology ,Inheritance is the mechanism which allows a Class 'A' to inherit properties of Class 'B' and we say 'A inherits from B' or in other words B is a 'Superclass'/'Parent class' while A is a 'Subclass'/'Child class'. A typical example of inheritance is a family tree which consists of son,father,grandfather,great grandfather and so on.The different types of Inheritance are:
1.Single Inheritance
2.Multiple Inheritance
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
In single inheritance, a class inherits implementation from only one super class. For example, if class B inherits from class A, class B will acquire all the members declared in class A.
B------>A(Parent)
In multilevel inheritance, a class inherits from a derived class (or subclass). For example, if class C inherits from class B, and class B inherits from class A, class C will acquire all the members declared in class B as well as all the members declared in class A.
C------->B------>A(Parent)
In hierarchical inheritance, many sub classes inherit from a single super class. For example, if classes B, C, and D inherit from class A, classes B, C, and D will acquire all the members declared in class A.
B------>A(Parent)C------>A(Parent)D------>A(Parent)
In multiple inheritance, a class inherits from several super classes. For example, if class C inherits from both class A and class B, class C will acquire all the members declared in class A as well as all the members declared in class B. Multiple inheritance is not directly supported by Java but through Interfaces one can.C------>A(Parent)C------>B(Parent)
A hybrid inheritance is a combination of any two of the above discussed inheritance types
1.Single Inheritance
2.Multiple Inheritance
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
In single inheritance, a class inherits implementation from only one super class. For example, if class B inherits from class A, class B will acquire all the members declared in class A.
B------>A(Parent)
In multilevel inheritance, a class inherits from a derived class (or subclass). For example, if class C inherits from class B, and class B inherits from class A, class C will acquire all the members declared in class B as well as all the members declared in class A.
C------->B------>A(Parent)
In hierarchical inheritance, many sub classes inherit from a single super class. For example, if classes B, C, and D inherit from class A, classes B, C, and D will acquire all the members declared in class A.
B------>A(Parent)C------>A(Parent)D------>A(Parent)
In multiple inheritance, a class inherits from several super classes. For example, if class C inherits from both class A and class B, class C will acquire all the members declared in class A as well as all the members declared in class B. Multiple inheritance is not directly supported by Java but through Interfaces one can.C------>A(Parent)C------>B(Parent)
A hybrid inheritance is a combination of any two of the above discussed inheritance types