Applying inheritance
As we learned in the previous section, inheritance creates an “is-a” relationship hierarchy. This enables base class functionality to be inherited and therefore available to subclasses, without any extra coding. Java uses two keywords in applying inheritance: extends
and implements
. Let’s discuss them now.
extends
This is the principle keyword that’s used and relates to both classes and interfaces. Regarding classes, we state that class Sub extends Base {}
. In this case, all of the non-private
members from the Base
class will be inherited into the Sub
class. Note that private
members and constructors are not inherited – this makes sense as both private
members and constructors are class-specific. In addition, Java prohibits multiple class inheritance. This means that you cannot extend from more than one class at a time. Regarding interfaces, we state that interface ChildInt extends
ParentInt {}
.
implements
While we...