Java Reference
In-Depth Information
class A
direct superclass of B
indirect superclass of C
derived from
class B
direct subclass of A
direct superclass of C
derived from
class C
direct subclass of B
indirect subclass of A
This shows just three classes in a hierarchy, but there can be as many as you like.
Let's consider a more concrete example. We 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, we could derive a Spaniel class that represented dogs that were spaniels:
class Spaniel extends Dog {
// Members of the Spaniel class...
}
The extends keyword that we 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 members of the
Spaniel class that appear in its definition. The breed would be "spaniel" for all instances of the class
Spaniel although in general the name for each spaniel would be different. The Spaniel class might have
some additional data members that characterize the specifics of what it means to be a spaniel. We will see in
a moment how we can arrange for the base class data members to be set appropriately.
Search WWH ::




Custom Search