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
Avoid Overusing Inheritance
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 which are generally more flexible.
Inheritance subtyping relationship
If inherits from , then . is a subtype of .
Multiple inheritance
When a subclass is able to inherit from multiple super classes. This leads to the diamond problem .
Java avoid multiple inheritance uses OOP Abstract Method. This can be proved with Proof by Contradiction (矛盾证明法).
”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
.