What is inheritance? Explain with an example.
Ans: Inheritance is a process of transferring or deriving properties of an
existing class to new class or classes. It is the process by which objects of one
class acquire the characteristics of object of another class. For example, all
vehicles in a class may share similar properties of having wheels and a
motor. In OOP, the concept of inheritance provides the idea of reusability.
The different types of inheritance are as follows:
Single inheritance: In this inheritance, a derived class is created from a
single base class. For example:
Class A
Class B
Single inheritance
Multi-level inheritance: A derived class is created from another
derived class and is called multi-level inheritance. For example:
Class A
Class B
Class C
Multi-level inheritance
Multiple inheritance: In this inheritance, a derived class is created
from more than one base class.
Class A Class B
Class C
Multiple inheritance
Multipath inheritance: In this inheritance, a derived class is created
from another derived classes and the same base class of another
derived classes.
Class A
Class C
Class B
Class D
Multipath inheritance
Hierarchical inheritance: More than one derived class are created
from a single base class, is called Hierarchical inheritance.
Class A
Class B Class C
Class D Class E Class F Class G
Hierarchical Inheritance
Hybrid Inheritance: Derived class is formed with the combination of
more than one types of inheritance is known as hybrid inheritance.
Class A
Class A
Class C Class B
Class D Class E Hybrid Inheritance