Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Inheritance

In object-oriented programming (OOP),Inheritance is the process of getting the properties or behavior of one class into another class.
Inheritance is a mechanism for code reuse and to allow independent extensions of the original application via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. inheritance only reuses implementation and establishes a syntactic relationship, not necessarily a semantic relationship (inheritance does not ensure behavioral subtyping). Class that gets the behaviour of another class is called derive class and class that give it's behavior to derive class is called base class.
In real world example, A son get surname and other information from his father so son is a derive class and father is a base class
There are three way by which we can inherit a class from another class.

1-Public: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
2-Protected:When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
3-Private:When deriving from a private base class, public and protected members of the base class become private members of the derived class.



Types of inheritance
1-single inheritance: In single inheritance, subclasses inherit the features of a single base class.
Single Inheritance example


2-Multiple inheritance: Multiple Inheritance allows a class to have more than one base class.
Multiple Inheritance example


3-Multilevel inheritance: In multilevel inheritance a derive class act as a base class clas for another class.
multilevel Inheritance example

4-Hybrid inheritance:
It is a mixture of all the above types of inheritance.
Hybrid Inheritance example

5-Hierarchical inheritance:In hierarchical inheritance a single class serves as a superclass (base class) for more than one sub class.
Hierarchical Inheritance example