Java Reference
In-Depth Information
9.1 Introduction
9.2 Superclasses and Subclasses
9.3 protected Members
9.4 Relationship Between Superclasses and
Subclasses
9.4.1 Creating and Using a
CommissionEmployee Class
9.4.2 Creating and Using a
BasePlusCommissionEmployee Class
9.4.3 Creating a CommissionEmployee -
BasePlusCommissionEmployee
Inheritance Hierarchy
9.4.4 CommissionEmployee -
BasePlusCommissionEmployee
Inheritance Hierarchy Using protected
Instance Variables
9.4.5 CommissionEmployee -
BasePlusCommissionEmployee
Inheritance Hierarchy Using
private Instance Variables
9.5 Constructors in Subclasses
9.6 Class Object
9.7 (Optional) GUI and Graphics
Case Study: Displaying Text and
Images Using Label s
9.8 Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises
9.1 Introduction
This chapter continues our discussion of object-oriented programming (OOP) by intro-
ducing inheritance , in which a new class is created by acquiring an existing class's mem-
bers and possibly embellishing them with new or modified capabilities. With inheritance,
you can save time during program development by basing new classes on existing proven
and debugged high-quality software. This also increases the likelihood that a system will
be implemented and maintained effectively.
When creating a class, rather than declaring completely new members, you can des-
ignate that the new class should inherit the members of an existing class. The existing class
is called the superclass , and the new class is the subclass. (The C++ programming language
refers to the superclass as the base class and the subclass as the derived class .) A subclass
can become a superclass for future subclasses.
A subclass can add its own fields and methods. Therefore, a subclass is more specific
than its superclass and represents a more specialized group of objects. The subclass exhibits
the behaviors of its superclass and can modify those behaviors so that they operate appro-
priately for the subclass. This is why inheritance is sometimes referred to as specialization .
The direct superclass is the superclass from which the subclass explicitly inherits. An
indirect superclass is any class above the direct superclass in the class hierarchy , which
defines the inheritance relationships among classes—as you'll see in Section 9.2, diagrams
help you understand these relationships. In Java, the class hierarchy begins with class
Object (in package java.lang ), which every class in Java directly or indirectly extends (or
“inherits from”). Section 9.6 lists the methods of class Object that are inherited by all
other Java classes. Java supports only single inheritance , in which each class is derived
from exactly one direct superclass. Unlike C++, Java does not support multiple inheritance
(which occurs when a class is derived from more than one direct superclass). Chapter 10,
Object-Oriented Programming: Polymorphism and Interfaces, explains how to use Java
interfaces to realize many of the benefits of multiple inheritance while avoiding the associ-
ated problems.
 
 
Search WWH ::




Custom Search