Summary
In this chapter, we examined one of the cornerstones of OOP, namely inheritance. Inheritance defines an “is-a” relationship between the sub- and parent classes – for example, Fox
“is-a” Animal
, and Train
“is-a” Vehicle
. Inheritance promotes code reuse as inheritable base class members are automatically available to subclasses. Class inheritance is enabled via the extends
keyword and interface inheritance is enabled via the implements
keyword.
Regarding methods, the subclasses are free to override (replace) the base class implementation. This is how we enable another cornerstone of OOP, namely polymorphism.
Polymorphism is a feature where the instance method from the object is only selected at runtime. Hence, other terms for polymorphism are “late binding,” “runtime binding,” and “dynamic binding,” For polymorphism to work, the signature of the instance method in the subtype must...