WHAT IS?
Is the procedure in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class.
There are 5 types of inheritance:
- Single Inheritance: This is a form of inheritance in which a class inherits only one parent class. This is the simple form of inheritance and hence also referred to as simple inheritance.Here the class Child is inheriting only one class Parent, hence this is an example of Single inheritance.
- Multiple Inheritance: An inheritance becomes multiple inheritances when a class inherits more than one parent class. The child class after inheriting properties from various parent classes has access to all of their objects.Here we have one Child class which is inheriting properties of three-parent classes Parent_1, Parent_2, and Parent_3. All the classes have different functions and all of the functions are called using the object of the Child class.
- Multi-level Inheritance: For example, a class_1 is inherited by a class_2 and this class_2 also gets inherited by class_3 and this process goes on. This is known as multi-level inheritance. Let’s understand with an example:
- Hierarchical Inheritance: In this, various Child classes inherit a single Parent class. The example given in the introduction of the inheritance is an example of Hierarchical inheritance since classes BMW and Audi inherit class Car.
For simplicity let’s look at another example: - Hybrid Inheritance: When there is a combination of more than one form of inheritance, it is known as hybrid inheritance. It will be more clear after this example:
Comentarios
Publicar un comentario