Thu Aug 25 2022
Inheritance in OOPs
Inheritance
Object-oriented programming creates reusable patterns of code to curtail redundancy in development projects. OOPs achieves recyclable code through inheritance. And Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object and create another class. A class that is used as the basis for inheritance is called a superclass or base class and the class that inherits from a superclass is called a subclass or derived class. Inheritance allows us to create classes that are built upon existing classes, to specify a new implementation to maintain the same behavior, to reuse code and to independently extend original software via public classes and interfaces.
Parent Classes - Parent or base classes create a pattern in which child or subclasses can be based on. Parent classes allow you to create child classes through inheritance without having to write the same code over again each time. You can make any class into a parent class, so they are each fully functional classes in their own right rather than just templates.
Child Classes - Child or subclasses are classes that will inherit from the parent class, means that each child class will use the methods and variables of the parent class.
Modes of Inheritance
Public mode - If you derive a subclass from a public base class then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. Private members of the base class will never get inherited in the subclass.
Protected mode - If you derive a subclass from a protected base class then both public member and protected members of the base class will become protected in the derived class. Private members of the base class will never get inherited in the subclass.
Private mode - If you derive a subclass from a private base class then both public member and protected members of the base class will become Private in the derived class. Private members of the base class will never get inherited in the subclass.
Types of Inheritance
OOPs supports the six types of inheritance. There are mentioned below -
Single Inheritance
When a Derived Class to inherit properties and behavior from a single Base Class, it is called as single inheritance.
Multi-level inheritance
A derived class can also inherit by another class, that means when a derived class again will be inherited by another class then it creates a Multiple Levels.
Multipath inheritance
In this inheritance, a derived class is created from another derived class and the same base class of other derived classes.
Multiple inheritance
Multiple Inheritances is that in which a Class inherits the features from two Base Classes When a Derived Class takes Features from two Base Classes.
Hybrid inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance.
Hierarchical inheritance
More than one derived class are created from a single base class, is called Hierarchical Inheritance.
Advantages
-
Reduces code redundancy and source code size.
-
Provides code re-usability and improves code readability.
-
The code is more manageable and divided into parent and child classes.
-
Supports code extensibility by overriding the base class functionality within child classes.
Disadvantages
-
Base class and child classes are tightly combined in Inheritance. So, If you change the parent class code, it will affect to the all the child classes.
-
In the class hierarchy, many data members remain unused and the memory allocated to them is unutilized, and it affects the performance of your program if you have implemented inheritance incorrectly.