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 subclassDog
. In this scenario, aDog
is anAnimal
. This meansDog
inherits all the characteristics (methods and fields) ofAnimal
. During compilation, aDog
object can be assigned theAnimal
data type and utilise methods defined inAnimal
, potentially with modifications through method overriding in theDog
class.This concept promotes polymorphism, allowing
Dog
objects to be treated as instances of their superclassAnimal
.