Tuesday, 28 May 2013

Inheritance And Its Types

As described in the previous post, Inheritance is the mechanism of deriving a new class from an existing class. It is also known as derivation. The derived class is also known as subclass and the class from which it is derived is known as super-class or base class.There are basically five types of inheritance which are as follows :
  1. Single Inheritance - If only one class is derived directly from the existing class than the inheritance is known as single inheritance. In this there is only one base class and only one derived class or base class. For example :  class 'B' is derived from class 'A' than A is super class and B is derived class. 
  2. Multilevel Inheritance - If a class is derived from another class which itself is derived from some other class than this type of inheritance is known as multilevel inheritance. In this there are two base class and two derived class. For example : class 'C' is derived from class 'B' and 'B' itself is derived from class 'A'.
  3. Multiple Inheritance - If a class is derived directly from two or more classes than this type of inheritance is known as multilevel inheritance. In this there is only one derived class and two or more base classes are there. For example : class 'D' is derived directly from class 'A' , 'B' and 'C'.
  4. Hierarchical Inheritance - If the derivation of class is in the form of hierarchy in which two classes are derived from one class and the two derived classes are the base class of some other classes too, than this type of inheritance is known as hierarchical inheritance. 
  5. Hybrid Inheritance - If a class derived by two class via multilevel inheritance and it is also derived from some other  class which are different from those two class, than this type of inheritance is known as hybrid inheritance. For example : 'C' is a class derived from 'B' and 'D' and 'B' is derived from class 'A'.
          These were the introduction about Inheritance. More details with examples will be discussed in the upcoming posts.                                                            


No comments:

Post a Comment