Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Subclasses, Superclasses, and Inheritance

In Java, as in other object-oriented programming languages, classes can be derived from other classes. For example, suppose you had a class named MouseEvent that represented the event when a user pressed the mouse button in a GUI window environment. Later, while developing your application, you realize that you also needed a class to represent the event when the user pressed a key on the keyboard. You could write the KeyboardEvent class from scratch, however, you would be duplicating a lot of code because the MouseEvent and the KeyboardEvent classes share some states and behaviours: the time that the event occurred, which mouse button or key was pressed, and so on. Rather than re-invent the wheel, you can decide to take advantage of subclassing and inheritance...so you create a class Event from which both MouseEvent and KeyboardEvent derive.

[PENDING: image here].

Event is the superclass of both MouseEvent and KeyboardEvent. MouseEvent and KeyboardEvent are both subclasses of Event. Event contains all of the state and behaviour common to both mouse events and keyboard events (time stamp, for example). Subclasses, such as MouseEvent and KeyboardEvent, inherit state and behaviour from their superclass. For example, MouseEvent and KeyboardEvent would both inherit the time stamp attribute from the Event class.

Subclasses may override some of their inherited features. For example, a KeyboardEvent can represent hundreds of different keystrokes ("a" and "b", for example) and a MouseEvent typically only represents a few different mouse presses (left mouse, right mouse, drag). Each Event subclass must override some state and behaviour to implement their specific event possibilities.

Subclasses may also add to the features that they inherit. A KeyboardEvent can actually represent keystroke combinations ("a" vs. "Shift A" or "Control A", for example) and must provide a new mechanism for handling these possibilities.

As you can see, inheritance provides a powerful and natural mechanism for organizing and structuring software programs putting the most general classes higher in the class hierarchy and the most specific classes lower in the class hierarchy. In addition, because classes inherit state and behaviour from their superclasses you don't have to write that code again--inheritance allows you to reuse code over and over again in each subclass you create. [PENDING: put something in here about class hierarchies and that in the remaining pages of this section "superclass" refers to the superclass AND all the things that it inherits from its superclass...]

Creating a Subclass

To a subclass of another class use the extends clause in your class declaration. (The Class Declaration explains all of the components of a class declaration in detail.) As a subclass, your class inherits various member variables and methods from its superclass.

Writing Final Classes and Methods

Sometimes, for security or design reasons, you want to prevent your class from being subclassed. Or, you may just wish to prevent certain methods within your class from being overriden. In Java, you can achieve either of these goals by marking the class or the method as final.

Writing Abstract Classes and Methods

On the other hand, some classes are written for the sole purpose of being subclassed (and are not intended to ever be instantiated). These classes are called abstract classes and often contain abstract methods.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces