Abstract


  • Extending from existing OOP Class, the child classes take up all the properties and methods of their parent classes, building up on existing abstraction

Important

Inheritance should be used only when a clear hierarchical relationship and shared behaviours exist between the parent (or base) class and the child (or derived) class.

Otherwise, consider using interfaces and composition.

”Is-a” Relationship


  • “A subclass is a superclass”, we mean that the subclass inherits from the superclass. This implies that the subclass can be used wherever the superclass is expected, adhering to the “is-a” relationship

A less abstracted explanation

Consider a superclass Animal and a subclass Dog. In this scenario, a Dog is an Animal. This means Dog inherits all the characteristics (methods and fields) of Animal. During compilation, a Dog object can be assigned the Animal data type and utilise methods defined in Animal, potentially with modifications through method overriding in the Dog class.

This concept promotes polymorphism, allowing Dog objects to be treated as instances of their superclass Animal.