Object-Oriented Programming Language (OOPS-2)
Inheritance
- Inheritance is a powerful feature in Object-Oriented Programming.
- Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance, the information is made manageable in a hierarchical order.
- The class which inherits the properties of the other is known as ​subclass (derived class or child class)​ and the class whose properties are inherited is known as ​superclass​ (base class, parent class)​.
Now we want to create ​Male​ and ​Female​ classes. Both males and females are humans and they share some common properties (like ​height​, ​weight​, ​age​, etc) and behaviors (or functionalities like ​eating()​, ​sleeping()​, etc), so they can inherit these properties and functionalities from the ​Human​ class. Both males and females also have some characteristics specific to them (like men have short hair and females have long hair). Such properties can be added to the ​Male​ and ​Female classes separately.
This approach makes us write less code as both the classes inherited several properties and functions from the superclass, thus we didn’t have to re-write them. Also, this makes it easier to read the code.
0 Comments