Learn-Object_Oriented_programming-with-us

Learn By Topic:

          

Class-objects

          

Inheritance

          

Polymorphism

          

Encapsulation

          

Abstraction

          

          

Click here to toggle b/w normal and focus mode: FOCUS MODE

What is Inheritance?

Advantages of Inheritance

Below are listed a few pros of inheritance:

  1. With the help of inheritance, we need not create and define data members and functions recursively. We code once in a class, and they can inherit all properties of data members and functions in the subsequent subclass. This feature also helps in effective dynamic programming.

  2. Frequent use of code written once, i.e. code reusability.

  3. One superclass can be used for the number of subclasses in a hierarchy.

  4. No changes to be done in all base classes; just do changes in parent class only.

  5. Inheritance is used to generate more dominant objects.

  6. Inheritance avoids duplicity and data redundancy.

  7. Inheritance is used to avoid space complexity and time complexity.

Different Types of Inheritance

OOPs support the five different types of inheritance as given below :

Single inheritance

In this inheritance, a derived class is created from a single base class.

In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.

Multi-level inheritance

In this inheritance, a derived class is created from another derived class.

In the given example, childClass_1 inherits the properties and behavior of ParentClass and ChildClass_2 inherits the properties and behavior of ChildClass_1 . So, here ChildClass_1 is the parent class of ChildClass_2 and class ParentClass is the parent class of ChildClass_1. So, here class ChildClass_2 implicitly inherits the properties and behavior of class ChildClass_1 along with Class ParentClass i.e there is a multilevel of inheritance.

Multiple inheritance

In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc. and Java Language.

In the given example, ChildClass inherits the properties and behavior of ParentClass_1 and ParentClass_2 at same level. So, here ParentClass_1 and ParentClass_2 both are the parent classes for ChildClass.

Hierarchical Inheritance

In this inheritance, more than one derived classes are created from a single base class and futher child classes act as parent classes for more than one child classes.

In the given example, ParentClass has two childs class's i.e ChildClass_1 and ChildClass_2 . From a single parent class more than one child class inheriting is called Hierarchical inheritance.

Hybrid inheritance

This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.

I hope you're clear with basic idea of what is inheritance, uses of inheritance and type of inheritance. We'll extend out knowledge on inheritance by discussing a simple example in next sections.

Click HERE for part-2 of Inheritance where we will be discussing a real life example of all types of inheritance



Thank you. Peace ✌️.


Made with ❤️ by SasiVatsal and Mowli