您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Content Index

Content Page # 29

Class hierarchies

When a new class is defined in terms of an existing class, it automatically inherits all the attributes and operations of its parent. The parent class is called the direct superclass, and of course it too may have been defined in terms of another class. The new class defined as a specialisation of its direct superclass is called a subclass. A subclass may have many superclasses, i.e. its direct superclass, and then the direct superclass's direct superclass and so on.

In such a way a class hierarchy is built. Class hierarchies can be shallow (just 2 or 3 levels of sub- and super-classes), or they can be very deep (10, 20 or even more levels of sub- and super-classes.

A note on misleading terminology:

In the normal use of English language we tend to think of super as meaning more and sub as meaning less. However, in the case of superclasses and subclasses the meanings here the reverse -- since a subclass is an extension of some class. When referring to object inheritance the prefixes "super" and "sub" simply mean "higher" and "lower" in the class hierarchy.

A particular level of a class hierarchies can also be either narrow (whereby a class has only a few subclasses) or very broad (whereby a class has many subclasses).

We might create the following hierarchy of classes for our public library:

LibraryItem
TextItem
FictionText
NonFictionText
Recording
Video
AudioCassette
CD
AudioCD
CDROM
DVD

In this example we have at the top of the hierarchy the superclass LibraryItem. This superclass has two subclasses: TextItem and Recording.

The class TextItem is itself the superclass of its subclasses FictionText and NonFictionText.

The class FictionText has the direct superclass TestItem. It also has a superclass LibraryItem. We can also say the FictionText is a subclass of LibraryItem, as well as being a subclass of TextItem. This is the reason for using the more precise terms direct superclass and direct subclass in certain situations. However, since most of the time it is the direct superclasses and subclasses that are referred to, the term direct is not also used, so a reader of a book or model or program documentation may have to infer or seek clarification as to whether a direct superclass or subclass was intended when just the phrase superclass or subclass is used.

We could define our LibraryItem class to have operations and attributes that are common to all kinds of library item, then define particular operations and attributes for each subclass. For example

  • Record objects might have extra totalDuration and numberOfTrack attributes.
  • FictionText objects might have an extra genre attribute and operation setRebindingDate()

By the way, some classes never have their own instances, only subclasses. Such classes are called abstract. The class LibraryItem is such an abstract class — one would never have an instance of LibraryItem, but one would have an instance of some subclass of LibraryItem. Another example of an abstract class is Bird — any particular bird is not an instance of the class Bird directly, it is an instance of some subclass, such as Sparrow, Blackbird, BaldEagle etc.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg