Java Reference
In-Depth Information
Chapter 3. Extending Classes
I am, in point of fact, a particularly haughty and exclusive person,
of pre-Adamite ancestral descent. You will understand this when I
tell you that I can trace my ancestry back to a protoplasmal prim-
ordial atomic globule.
Gilbert and Sullivan, The Mikado
The quick tour ( Chapter 1 ) described briefly how a class can be extended,
or subclassed, and how an object of an extended class can be used
wherever the original class is required. The term for this capability is
polymorphism, meaning that an object of a given class can have multiple
forms, either as its own class or as any class it extends. The new class
is a subclass or extended class of the class it extends; the class that is
extended is its superclass.
The collection of methods and fields that are accessible from outside a
class, together with the description of how those members are expec-
ted to behave, is often referred to as the class's contract. The contract is
what the class designer has promised that the class will do. Class exten-
sion provides two forms of inheritance:
inheritance of contract or type, whereby the subclass acquires
the type of the superclass and so can be used polymorphically
wherever the superclass could be used; and
inheritance of implementation, whereby the subclass acquires the
implementation of the superclass in terms of its accessible fields
and methods.
Class extension can be used for a number of purposes. It is most com-
monly used for specialization where the extended class defines new be-
havior and so becomes a specialized version of its superclass. Class ex-
tension may involve changing only the implementation of an inherited
method, perhaps to make it more efficient. Whenever you extend a class,
you create a new class with an expanded contract. You do not, however,
 
Search WWH ::




Custom Search