Specialized classes inherit the
properties and methods of the
parent or base class.
A dog is a mammal
A car is a vehicle
A Button is a Control
Specialization is implemented in Java
through inheritance. The extends
keyword is used to implement this
relationship between classes.
class Dog extends (specializes)
Mammal is the base class (superclass)
Dog is the derived class (subclass)
Code Reuse with Inheritance
Inheritance permits easy reuse of
A DeadBall class can be derived from a
Do not have to recode Ball
characteristics in DeadBall class
private fields are inaccessible to
outside classes (including derived
protected fields are accessible to
derived classes, but not to outside
The super keyword invokes the base
Must be called from constructor of
Must be first statement within constructor
More Rules forUsing super
Call must match the signature of a valid
signature in the base class
Implicitly called in the constructor if
omitted, so the base class must have a
If derived class does not define a
constructor, the compiler provides one
and calls super automatically
An object can take many forms.
Method overloading is one type of
Object polymorphism treats specialized
objects as if they are instances of a more
A method expecting a Car object can only
accept Car objects
A method expecting Vehicle objects can
accept any object derived from the
Factoring to the Base Class
Factor common characteristics of
multiple classes up into a base class.
HWall and VWall classes have similar
characteristics and methods.
Factor commonalities to a Wall class
Override some methods to specialize the
HWall and VWall classes
Abstract Base Classes
The classes describe what all derived
classes have in common.
Not instantiated (causes an exception)
Use the abstract keyword when defining
abstract base classes and methods
Abstract methods are not implemented.
Derived classes must provide method