6.1. 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 base class because it forms the base for the definition of the derived class. The original 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. Figure 6-1
This shows just three classes in a hierarchy, but there can be as many as you like.
Let's consider a more concrete example. You could define a class Dog that could represent a dog of any kind:
class Dog {
// Members of the Dog class...
}
This might contain a data member identifying the name of a particular dog, such as Lassie or Poochy, and another data member to identify the breed, such as Border Collie or Pyrenean Mountain Dog. From the Dog class, you could derive a Spaniel class that represented dogs that were spaniels:
class Spaniel extends Dog {
// Members of the Spaniel class...
}
The extends keyword that you use here identifies that Dog is a base class for Spaniel, so an object of type Spaniel will have members that are inherited from the Dog class, in addition to the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access