Inheritance

Inheritance is the passing of traits from parents to their offsprings. In computer science, we have parent class and child class. Child class inherit the attributes of parent class.

  • Parent Class is also called Base class or Super class.
  • Child Class is also called Derived class or Sub class.

Note: It is a custom to start the name of class with capital letter. But if you are fitting well with small letter, there is no problem.

Protected Data Members

Before we go into details, we should study about protected data members. When we inherit class using public access modifier, child class can access only public members of parent class. But if we want to access private members? Actually, we can not access private members even in child class. For this purpose, we make data members protected. The protected data members can not be accessed in main function; they can only be accessed in child class.

We have studied access specifiers of class members. Access specifiers are also used in inheritance. There are three modes of inheritance based on access specifiers.

  1. Public mode
  2. Private mode
  3. Protected mode

Let's explore some details.

Access Specifiers in Inheritance

Public access specifier

When a class is inherited using a public access specifier, all public members of the parent class become public members of the child class.

Private access specifier

When a class is inherited with a private access specifier, all members of the parent class (whether they are public, protected, or private) become private members of the child class.

Protected access specifier

When a class is inherited with a protected access specifier, all public members of the base class become protected members of the derived class. Protected members of the base class can be accessed directly through objects of the derived class or within member functions of the derived class.

Levels of Inheritance

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multi-level Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance

One parent class has one child class.

Multiple Inheritance

Multiple inheritance allows a class to inherit from more than one base class. One child and many parents.

Multi-level Inheritance

In multilevel inheritance, a child class is created from another child class. If we have class1, class2 and class3, class1 is the parent of class2 and class2 is the parent of class3 and so on. It is like grand-parent(child1), parent(class2), child/grandson(class3).

Hierarchical Inheritance

In hierarchical inheritance, multiple base classes inherit from the same parent class. Many child, one parent.

Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and single inheritance. It involves the use of multiple base classes and is generally seen in complex class hierarchies.

Inheritance and Constructor

When we have a base class and a derived class, we need to pass arguments from derived class to base class.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min