Inheritance

Get familiar with the concept of inheritance and its type with implementation.

Definition

Inheritance provides a way to create a new class from an existing class. The new class is a specialized version of the existing class such that it inherits all the public attributes (variables) and methods of the existing class. The existing class is used as a starting point or base to create the new class.

The IS-A relationship

After reading the definition above, the next question that comes to mind is, “when do we use inheritance?” Wherever we come across an IS-A relationship between objects, we can use inheritance.

The IS-A relationship

So, from the description of inheritance above, we can conclude that we can build new classes by extending existing classes.

Existing Class

Derived Class

Shape


Animal


Vehicle

Square


Dog


Car

Let’s look at the figure below to visualize some examples where an IS-A relationship ...

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.