Inheritance in OOP
One of the most important features of object oriented programming is inheritance . If we look for a real world example of inheritance, we can easily understand the example of a father or son relationship . Property of the father is easily acquired by his son. That's exactly what inheritance does. Which means a son inherits the property of his father. Moreover, the son also inherited the diseases and other genetic information from his biological father.
What does Inheritance mean?
Inheritance is the approach by which objects of one class achieve the attributes of objects of another class. Inheritance is the mechanism of creating new classes called derived classes, from the existing classes, called base class.
The derived classes inherit all the properties of the base classes, but the derived classes have freedom of adding their own property in the class as well. But the properties of the base classes are constant, which means we can't change the properties of the base class.
Sub class - The class which takes the properties of other classes is called subclass Or derived class.
Super class - The class whose attributes are inherited by derived class is known as super class or base class.
Role of Inheritance in Object Oriented Programming
Inheritance is a natural form to model the world or a domain of discussion, and this determines a natural model for Object-oriented analysis (OOA), object-oriented design (OOD) and constant in object-oriented programming (OOP) as well. Inheritance is a very common approach for the artificial intelligence (AI) domain, where semantic networks take advantage of inheritance to understand the world by using classes and concepts for generalization and categorization by decreasing the real-world's inherent complexity.
It also permits for code and structural reuse. In the computer class all routines and structure available in class computers are accessible to all subclasses. This type of reutilization makes full use of the code and structure.
Point to be remembered
Semantic networks - semantic networks or a frame network is information that shows semantic relations between concepts in the network.
Modes of Inheritance
Basically, There are three modes of inheritance are -
- Public Mode
- Protected Mode
- Private Mode
- Public Mode - If the base class is inherited publicly then the public members of the base class become public members of the subclass . Member functions and objects of the subclass (derived class) consider these derived members in such a way that they are defined in the subclass separately. Everyone knows that the public members of a class can be accessed by the objects of the class. The public members of the super class that are inherited publicly can be accessed by the objects of a subclass using dot operator.
- Protected Mode - If the super class is inherited in protected mode then the protected member of the superclass becomes the protected member of the base class. Dot operator doesn't work here to access protected members.
- Private Mode - If the superclass is inherited privately then the public members of the base class become private members of the subclass and the protected members of the superclass also become private members of the sub class.
Types of inheritance in OOP
Single Inheritance - only one class is enabled to inherit in single inheritance. A derived class with only one base class is called single inheritance.
Syntax of Single Inheritance
Class derived_class : visibility_mode base_class
{
// Body of derived class
};
It represents B is a base class and D is a derived class
Single Inheritance |
Multiple Inheritance - A derived class with several base classes is known as multiple inheritance. Multiple inheritance gives freedom of Combining the features of several base classes together.
Syntax of Multiple Inheritance
class D : visibility B1, visibility B2
{
// body of derived class
};
Multiple Inheritance |
Hierarchical Inheritance - The one superclass or base class inherited by multiple subclasses is known as hierarchical inheritance.
Hierarchical Inheritance |
Multilevel Inheritance - The method of deriving a class from another "derived class" is called multilevel inheritance.
Syntax of multilevel inheritance
class A // base class
{
…..
};
class B : public A //derived class from A
{
……
};
class C : public B // C derived from B
{
……
};
Multilevel Inheritance |
Hybrid inheritance - When a program needs two or more than two types of inheritance then this is called hybrid inheritance.
Advantages of Inheritance
- We can reuse code again and again. It means inheritance provides code reusability.
- Save our time and effort because we don't have to write the main code again.
Disadvantages of Inheritance
- Improper or less knowledge of inheritance gives wrong output.
- Inheritance gives the freedom of coupling between the superclass and subclass and if we change anything in superclass it will affect the subclasses of that superclass.
Role of inheritance in Reusability of software
Once the base class is written and debugged, we can use it again and again without any problem. Because inheritance gives us this feature of reusability . We can use the code of the base class in its derived class without any problem .