Header Ads

What is inheritance and its type? Advantages and disadvantages.

Inheritance

Inheritance is a phenomenon of creating new classes from the existing ones. It is one of the best features of object oriented programming. The classes from which new classes are created are known as base class or super class or parent class. The classes which are derived from another class(es) are known as derived class. 
Image courtesy:Flickr

Advantages of inheritance:

  1. Saves time and efforts: Inheritance saves time and efforts of the user as the programmer need not write the repeated codes again and again.
  2. Reusability: The class once created by the programmer can be repetitively used in any part of the program.
  3. Reliability: Increases the reliability of the program.
  4. User can create as many derived classes while making desired changes in each class.
  5. Debugging:Inheritance allows the opportunity to identify the problem as it is easy to debug a program when it is divided into parts.

Disadvantages of inheritance:

  1. Usage of inheritance makes the program more complicated.
  2. The base class and derived class are tightly coupled and cannot be used independently.
  3. Any change in the superclass affects all the derived classes as well.

Types of inheritance:


1. Single inheritance: It is the ability of a derived class to inherit its properties from its parent class.
The process of creating a new class from a base class is known as single inheritance and the derived class is known as singly derived class.

2. Multi-level inheritance: In multi-level inheritance a derived class becomes parent class of another derived class. as shown in figure class B is inherited by class A and is the parent class of class C.

3. Hierarchical inheritance: In hierarchical inheritance one base class serves as parent class for more than one derived class.
4. Multiple inheritance: In multiple inheritance a derived class id derived from more than one base class.
5. Hybrid inheritance: It is a combination of multiple and hierarchical inheritance. As shown in figure class A,B and C exhibits hierarchical inheritance whereas class B, C and D exhibits multiple inheritance.


No comments:

CSE Solved. Powered by Blogger.