Inheritance Extending Classes

Inheritance is the capability of one class to inherit properties from another class as a child inherits some properties from his or her parents. The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money and efforts of writing the code again. Without redefining the old class, you can add new properties to desired class and redefine an inherited class member function.

Need for Inheritance

Inheritance is one of the important concepts of object-oriented language. There are several reasons why this concept was introduced in object oriented language. Some major reasons are:

  • The capability to express the inheritance relationship which ensures the closeness with the real world model.
  • Idea of reusability, i.e., the new class can use some of the features of old class.
  • Transitive nature of inheritance, i.e., it can be passed on further.

Different Forms of Inheritance

The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class and new one is called the derived class. There are various forms of inheritance:

  1. Single inheritance: A derived class with only one base class is called single inheritance.
  2. Multiple inheritance: A derived class with several base classes is called multiple inheritance.
  3. Multilevel inheritance: The mechanism of deriving a class from another derived class is called multilevel inheritance.
  4. Hierarchical inheritance: One class may be inherited by more than one classes. This process is known as hierarchical inheritance.
  5. Hybrid inheritance: It is a combination of hierarchical and multiple inheritance.

Defining Derived Class

A derived class is defined by specifying its relationship with the base class using visibility mode.

The general form of defining a derived class is:

class derived_class : visibility_mode base_class
{
_________________
_________________ // members of derived class.
};

The colon indicates that the derived_class is derived (inherits some property) from base_class.

Visibility Modes

It can be public, private or protected. The private data of base class cannot be inherited.

  1. If inheritance is done in public mode, public members of the base class become the public members of derived class and protected members of base class become the protected members of derived class.
  2. If inheritances is done in a private mode, public and protected members of base class become the private members of derived class.
  3. If inheritance is done in a protected mode, public and protected members of base class become the protected members of derived class.

The data present in private section of base class cannot be inherited. The difference between private and protected section is that data present in protected section can be inherited. Otherwise both the section cannot be accessed by the object of the class.

Abstract Class

An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class (to be inherited by other classes). It is a design concept in program development and provides a base upon which other classes may be built.