×

Search anything:

4 Advantages and 6 disadvantages of Inheritance

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article at OpenGenus, we have listed and explained the advantages and disadvantages of Inheritance.

Table of contents:

  1. Advantages of Inheritance
  2. Disadvantages of Inheritance

Advantages of Inheritance

The 4 Advantages of Inheritance are:

  1. Code reuse
  2. Code Design
  3. Polymorphism
  4. Encapsulation

Following are brief detail on each point:

  • Code reuse: The major advantage of Inheritance when used correctly is code reuse. The common parts of multiple classes can be reused.
  • Code Design: OOP is a standard and designing good code is important for code collaboration and if the code is to be worked on by multiple developers. When used correctly, inheritance is a pillar of good code design.
  • Polymorphism: This is the idea that different classes can be considered as objects of the same class. This is only possible through inheritance.
  • Encapsulation: This is the idea that parts of implementation are hidden from the external user. This is possbile through multiple levels of inheritance and private classes.

Disadvantages of Inheritance

Following are the 6 disadvantages of Inheritance:

  1. Tight Coupling
  2. Fragile Base Class problem
  3. Diamond Inheritance Problem
  4. Inflexibility
  5. Overuse of Inheritance
  6. Increase in code complexity

Following are brief detail on each point:

  • Tight Coupling: Any change in the Parent class will be reflected in the child classes and this can have unintended functionality. It is important to come up with correct class design right at the beginning to avoid this issue.
  • Fragile Base Class Problem: If the derived classes are not designed properly provided the base class is not as complete as it should be, then changes to the base class can have severe impact on the derived classes.
  • Diamond Inheritance Problem: When multiple classes B and C inherit from a common class A and another class D inherits from 2 classes that is B and C. The problem comes in the case which function should be overridden: the function implementation of B or C. Due to this issue, some programming languages like Java do not provide support for Multiple Inheritance.
  • Inflexibility: An independent class is more flexible than a derived class as a derived class need to follow the structure of the base class while an independent class need to follow only backward compatibility.
  • Overuse of Inheritance: Designing code is a skill and in an attempt to make code modular and use OOP principles, some programmers tend to overuse inheritance.
  • Increase in code complexity: Overuse of ineritance not only results in bad code design but only has an impact on the code complexity making collaboration difficult.

With this article at OpenGenus, you must have the complete idea of advantages and disadvantages of Inheritance.

4 Advantages and 6 disadvantages of Inheritance
Share this