Inheritance in Object-Oriented Programming
A real-world example of inheritance is a father and child relationship. The child inherits attributes from its father, such as height, eye color, and hair color, but at the same time that child has its own personality.
In Object-Oriented Programming, inheritance lets one class adopt the properties of another class. The inheriting class is called a sub-class or a child class. The original class is called the superclass or the parent class.
Depending on the programming language used, a keyword such as
extends is used on a class, along with the parent class name, in order to make it become a sub-class of another class.
An example of inheritance
Inheritance is all about the relationship between classes. An example of inheritance is
Clerk classes that inherit from an
Clerk classes will have access to all of the
Employee class properties, such as
employeeNumber. The child classes of the
Employee class can then have their own properties and methods that are exclusive to either Managers or Clerks. For example, the
Manager class may have a
The benefits of inheritance
Inheritance helps with the separation of concerns. Each child class manages its own logic rather than expecting the parent class to handle the logic for all of its children.
Inheritance also helps with code reusability. Multiple child classes can inherit the same attributes from their parent class without having to repeat them.