Ticker

6/recent/ticker-posts

OOPS (Object-Oriented Programming Language) Part-2

 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)​.
Let us take a real-life example to understand inheritance. Let’s assume that ​Human is a class that has properties such as ​height​, ​weight​, ​age​, etc and functionalities (or methods) such as ​eating()​, ​sleeping()​, ​dreaming()​, ​working()​, etc. 

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.


Post a Comment

0 Comments