Java Reference
In-Depth Information
Subclasses inherit all fields and methods of a superclass. Objects of subclasses have all fields
and methods declared in their own classes, as well as those from all superclasses. Inheritance
relationships can be used to avoid code duplication, to reuse existing code, and to make an ap-
plication more maintainable and extendable.
Subclasses also form subtypes, which leads to polymorphic variables. Subtype objects may be
substituted for supertype objects, and variables are allowed to hold objects that are instances of
subtypes of their declared type.
Inheritance allows the design of class structures that are easier to maintain and more flexible.
This chapter contains only an introduction to the use of inheritance for the purpose of improv-
ing program structures. More uses of inheritance and their benefits will be discussed in the fol-
lowing chapters.
Terms introduced in this chapter
inheritance, superclass (parent), subclass (child), is-a, inheritance hierarchy,
abstract class, subtype substitution, polymorphic variable, type loss, cast,
autoboxing, wrapper classes
Concept summary
inheritance Inheritance allows us to define one class as an extension of another.
superclass A superclass is a class that is extended by another class.
subclass A subclass is a class that extends (inherits from) another class. It inherits all fields
and methods from its superclass.
inheritance hierarchy Classes that are linked through inheritance relationships form an inher-
itance hierarchy.
superclass constructor The constructor of a subclass must always invoke the constructor
of its superclass as its first statement. If the source code does not include such a call, Java will
attempt to insert a call automatically.
reuse Inheritance allows us to reuse previously written classes in a new context.
subtype As an analog to the class hierarchy, types form a type hierarchy. The type defined by
a subclass definition is a subtype of the type of its superclass.
variables and subtypes Variables may hold objects of their declared type or of any subtype
of their declared type.
substitution Subtype objects may be used wherever objects of a supertype are expected.
This is known as substitution.
Object All classes with no explicit superclass have Object as their superclass.
autoboxing Autoboxing is performed automatically when a primitive-type value is used in a
context requiring a wrapper type.
Search WWH ::




Custom Search