What is Inheritance?
In this lesson, we'll be learning about the core concept of the object-oriented paradigm, i.e., Inheritance and why there is a need for it?
Why do We Need Inheritance?
In the classes chapter, we’ve covered the HAS-A
relationship. We know a class HAS-A
data members and member functions. Now, we want the data members, and member functions of the class are accessible from other classes. So, the capability of a class to derive properties and characteristics from another class is called Inheritance
. In inheritance, we have IS-A
relationship between classes e.g a car is a vehicle and a ship is a vehicle.
Let’s take the example of Vehicle
here.
Vehicle Class
In a Vehicle class, we have many data members like Make, Color, Year and Model. A Vehicle
HAS-A Model, Year, Color and Make.
Implementation of Vehicle
Class
Let’s look at the implementation of Vehicle
class: