Inheritance

What is Inheritance

Inheritance is one of the core concepts in OOP that allows us to create a new class based on an existing class. The new class inherits properties and behaviors from the existing class, allowing us to build specialized versions of the original class. It's like inheriting traits from your parents: you share some characteristics but also have unique ones.

Terminology

Here are some key terminologies in inheritance:

  • Base class (or parent class): The original class from which inheritance occurs.

  • Derived class (or child class): The new class that inherits properties from the base class.

Let's consider we have a car and a bike. The car is a sports car, and the bike is a heavy bike. Both of these vehicles share some properties, i.e., both have a model, both have a type and a color but they will also have some different characteristics. If we want to map them, we will create a Vehicle class and then create two separate classes for a bike and a car each. These separate classes will inherit from the main Vehicle class.

Notation

Let’s take a look at the notation for these two types.

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy