Java Reference
In-Depth Information
8.
Finally, you used the newly created getter and setter methods to read and modify the variables in a
safer way.
class inheritance
Java classes are organized in a hierarchical inheritance structure. Subclasses are derived from super-
classes. The class
Object
is the highest-level superclass; it has no parent class above it. All other
classes are either subclasses of
Object
or subclasses of subclasses of
Object
. This is illustrated in
Figure 7-2.
Object
ClassA
ClassB1ii
ClassC
ClassA1
ClassB1ii
ClassA2
ClassB1ii
ClassB1ii
figure 7-2
Inheritance represents “is a” relationship. For example, a
Student
is a
Person
, so the class
Student
could be a subclass of the
Person
class. An
Employee
is a
Person
, too, so
Employee
could be
another subclass of
Person
. An undergraduate is a student and a graduate is a student, so you can
create two additional subclasses of the class
Student
.
Staff
and
Employee
might be subclasses of
the class
Employee
. A possible class hierarchy for this inheritance example is shown in Figure 7-3.
To indicate that a class is a subclass of another class, use the
extends
keyword in the class declara-
tion, for example,
public class Employee extends Person{ }
. Variables and methods that are
shared by all types of
Persons
can be placed in the
Person
class, and then each subclass can have
specialized variables and methods that are only used by that type or its subclasses. For example,
Search WWH ::
Custom Search