Inheritance is the capability of one class of things to derive its properties from another class of things. It is an essential object-oriented programming concept.
Let’s think of inheritance from a real-life perspective – take, for example, your family. You derive some of your characteristics from your father and some from your mother. Some of these characteristics are salient, while some are prominent like the color of your hair or eyes. Even further, your father and mother derive their characteristics from their parents.
Now, how exactly does this translate into code?
Well, we have all sorts of real-world relations that inheritance can describe in programming such that some features are salient while others are visible to the user clearly. This is implemented through inheritance, having different access levels (private, public and protected).
Let’s consider another example to understand this better – a class called ‘Shape’ defines the basic form of shapes like circles, triangles, and squares. Thus, classes ‘Circle’, ‘Triangle’ and ‘Square’ inherit from ‘Shape’ and add onto the basic structure on the basis of their individual features i.e. the specific areas, perimeters, etc of the particular shapes. Similarly, ‘Equilateral Triangle’, ‘Isosceles Triangle’ etc become derived classes from ‘Triangle’ with the specific where the definition for the sides changes accordingly.
Types of inheritance:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Inheritance has several advantages as follows: reusability, real-world relevance; time-saving; transitive nature; easier debugging.