In: Computer Science
7. What are the different forms of Inheritance? Give an example for each.
Object Oriented Programming supports 5 types of Inheritance as given below:
Single inheritance
Multi-level inheritance
Multiple inheritance
Hierarchical Inheritance
Hybrid Inheritance
1. Single inheritance
This is only 1 level of inheritance in which a derived class is created from a single base class. Example - In this class B(child class) is derived from the class A(Parent class)
2. Multi-level inheritance
In this type of Inheritance, Classes are inherited in the form of levels i.e., a derived class is created from another derived class. In the following example C is derived from B and B is derived from A.
3. Multiple inheritance
In this inheritance, a derived class is created from more than one base class. And thus this derived class will implement properties of all these Base classes. In the following example, Child class C is derived from Parents class A and Class B.
4. Hierarchical Inheritance
In this inheritance, more than one derived classes are created from a single base class and futher child classes act as parent classes for more than one child classes. In the following example, Class B and Class C are derived from the same parent class A
5. Hybrid Inheritance
This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance. For example:-