OOAD Princhiples Q/A #1


Question:What is inheritance? Discuss types of inheritance with example.

Answer:

Inheritance

Inheritance is the mechanism to allow a class A to inherit properties of class b i.e A inherits from B. Objects of class A thus have accesses to attribute and methods of class B without the need to redefine them. If class A inherits from class B , then B is called super class of A and A is called subclass of B. Supper classes are also called base classes or parents classes. Subclasses may be called derived classes or child classes.

Inheritance

Importance of inheritance

Inheritance is one of the most powerful features of object oriented programming. Most important advantages of inheritance are:

  1. Reusability - Inheritance allows deriving new classes from existing classes without modifying it. This helps in reusability of information in the child class as well as adding extra functionality in to it.

  2. Saves times and efforts - The concept of reusability achieved by inheritance saves a lot of programmer time and effort as the main code written can be reused in various situations as needed.

  3. Closeness with the real world - Inheritance allows the capability to express the inheritance relationship and ensures closeness with the real world models.

  4. Easy modification - Inheritance allows modification to be done in an easier manner.

  5. Transitive Nature of inheritance - If a class A inherits properties of another class B then all subclasses of A will automatically inherits the properties of B. This concept is called transitive nature of inheritance.

Syntax of defining a sub class from super class

class subclass: visibility_mode  superclass_name 
{
...
}

Here class is keyword. superclass_name is the neme of the derived class which is a valid identifier. Visibility modes are of three types public, private and protected.

Types of Inheritance

Various types of inheritance are supported by C++. These are:

  1. Single Inheritance - When a subclass inherits properties from one base class, it is called single inheritance. In single inheritance , there is only one base class and one derived class.

  2. Multiple Inheritance - When a subclass inherits properties from multiple base classes, it is known as multiple inheritance. In multiple Inheritance , there is only one derived class and several base classes.

  3. Hierarchical Inheritance - When two or more subclasses inherit properties from a single base class, It is known as Hierarchical inheritance . In hierarchical inheritance , the feature of one class may be inherited by more then one class.

  4. Multilevel Inheritance - In multilevel Inheritance , A class is derived from an already derived class. The transitive nature of inheritance is reflected by this form of inheritance.

  5. Hybrid Inheritance - When two or more type of inheritance are required to code a program, such type of inheritance is called hybrid inheritance. Figure shown below is a combination of multilevel and multiple inheritance.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements