- Inheritance is one of the important features of the oop(object-oriented programming).
- Inheritance is the process by which objects of one class acquire the properties of objects of another class in the hierarchy.
Advantages of Inheritance
- It ensures reusability of code
- It is very close to real life
- It saves memory space
- It saves time
- It increases the reliability of code
Access Labels
PRIVATE |
PUBLIC |
PROTECTED |
Private members of the base class are inaccessible to the derived class. |
Private members of the base class are inaccessible to the derived class. |
Private members of the base class are inaccessible to the derived class. |
Protected members of the base class become private members of the derived class. |
Protected members of the base class become protected members of the derived class |
|
Public members of the base class become private members of the derived class. |
Public members of the base class become public members of the derived class. |
Public members of the base class become protected members of the derived class. |
Types of inheritance
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Single Inheritance
- Single Inheritance is the simplest form of Inheritance.
- In the single Inheritance, one derived class inherits only from one base class.
Multiple Inheritance
- In the multiple Inheritance, single inheritance may inherit from more than one base class.
Hierarchical Inheritance
- In the hierarchical Inheritance, more than one derived class may inherit from the single base class.
Multilevel Inheritance
- In the multilevel Inheritance, the subclass acts as a base class for other classes.
Hybrid Inheritance
- Hybrid Inheritance is a combination of multilevel and hierarchical inheritance.
Base class and derived class
Example
class square{
....
....
....
}
class area:public square{
.....
.....
.....
}
class perimeter:public square
{
.....
.....
.....
}