Advantages:-
By distributing common code among numerous subclasses, inheritance helps applications contain fewer duplicate lines of code. When the same code appears in two classes that are connected to one another, the hierarchy can typically be refactored to promote the shared code to a mutual superclass. Additionally, this usually leads to smaller, simpler compilation units and better code structure.
Because classes that inherit from the same superclass can be used interchangeably, inheritance can also make application code more adaptable to changes. Whenever a method's return type is the superclass.
Reusability: The ability to use base class public methods without having to recreate them Extensibility: Extending base class logic to accommodate derived class business logic Data hiding: The base class can choose to keep some data private so that it cannot be changed by the derived class
Overriding—Thanks to inheritance, we are able to construct useful implementations of base class methods in derived classes by substituting our own methods for those of the base class.
Disadvantages:-
- The extra time and effort required for the programme to navigate through all the levels of overloaded classes is one of the key drawbacks of inheritance. It will essentially require ten hops to go through a function defined in each of the classes that are ten levels of abstraction above a specific class.
- The main drawback of inheritance is the close coupling between the base and inherited classes. This implies that one cannot be used without the other.
- Additionally, over time, it becomes necessary to modify both base and derived classes when doing maintenance or adding new features. We will be impacted in both scenarios if a method signature changes (inheritance & composition)
- We will need to re-factor if a method in the "super class" or aggregate is deleted and we need to use that method. In this scenario of inheritance, things can become a little more tricky because while our programmes will still compile, their methods will no longer override those of the superclass. These techniques will develop into stand-alone techniques.