Java Reference
In-Depth Information
Chapter 6
Extending Classes and Inheritance
WHAT YOU WILL LEARN IN THIS CHAPTER
• How to reuse classes by defining a new class based on an existing class
• What polymorphism is and how to define your classes to take advantage of it
• What an abstract method is
• What an abstract class is
• What an interface is and how you can define your own interfaces
• How to use interfaces in your classes
• How interfaces can help you implement polymorphic classes
A very important part of object-oriented programming enables you to create a new class based on a class that
has already been defined. The class that you use as the base for your new class can be one that you have
defined, a standard class in Java, or a class defined by someone else — perhaps from a package supporting a
specialized application area.
This chapter focuses on how you can reuse existing classes by creating new classes based on the ones you
have and explores the ramifications of using this facility, and the additional capabilities it provides. You also
delve into an important related topic — interfaces — and how you can use them.
USING EXISTING CLASSES
Let's start by understanding the jargon. Defining a new class based on an existing class is called derivation .
The new class, or derived class , is referred to as a direct subclass of the class from which it is derived. The
original class is called a baseclass because it forms the base for the definition of the derived class. The origin-
al class is also referred to as a superclass of the derived class. You can also derive a new class from a derived
class, which in turn was derived from some other derived class, and so on. This is illustrated in Figure 6-1 .
FIGURE 6-1
 
 
Search WWH ::




Custom Search