Inheritance of classes, Why we need to inherit the classes

Inheritance of classes, Why we need to inherit the classes

What is the inheritance of class?

We can create child classes for a class. Child classes are called derived classes and parent classes are called base classes.

Why do we need to inherit the classes?

Lets an example of daily life.

Suppose John is a person with following properties;

  • John has resource. e.g, home, car, bank balance, and a big property.

Now John need his child for the following two reasons;

  • To support his daily work
  • To get the resources before and after his death.

So, this will leads the John to think that he should have a baby.

Similarly, in OOP, we need to create a child of the parent classes so that when we declare any variable or function in the parent class, it can be shared easily with his child class without wasting time and computer memory.

What are categories of inheritance?

There are two categories of inheritance;

  1. Single inheritance
  2. Multiple inheritances

Both of these are discussed in a separate tutorial.

Write the syntax to inherit the class from parent class?

Serial# Code
1 class child_class_name : Access_specifier Parent_class_name
2 {
3 Body of the class;
4 };

Line 1: 

  • the class is a keyword to start class. after class, there is a space
  • After space we put child class name, then :
  • After: we put access specifier for class(private, protected, public)
  • After access specifier, there is one space and then parent class name.
Test Your Understandings

1.class parent_class_name : Access_specifier child_class_name is the correct syntax for creating child class? YES / NO

Answer - Click Here:
NO, it is

class child_class_name : Access_specifier Parent_class_name

2. There are two categories of inheritance?

1…………….?

2……………..?

Answer - Click Here:
1. single inheritance, 2. Multiple inheritance

Topic Covered

Inheritance of classes, Why we need to inherit the classes.