26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 Object-Oriented <strong>Program</strong>ming 447<br />

Software reusability saves time in program development. It encourages reuse of<br />

proven and debugged high-quality software, thus reducing problems after a system<br />

becomes operational. These are exciting possibilities. Polymorphism enables us <strong>to</strong> write<br />

programs in a general fashion <strong>to</strong> handle a wide variety of existing and yet-<strong>to</strong>-be-specified<br />

related classes. Polymorphism makes it easy <strong>to</strong> add new capabilities <strong>to</strong> a system. Inheritance<br />

and polymorphism are effective techniques for dealing with software complexity.<br />

When creating a new class, instead of writing completely new instance variables and<br />

instance methods, the programmer can designate that the new class is <strong>to</strong> inherit the instance<br />

variables and instance methods of a previously defined superclass. The new class is<br />

referred <strong>to</strong> as a subclass. Each subclass itself becomes a candidate <strong>to</strong> be a superclass for<br />

some future subclass.<br />

The direct superclass of a class is the superclass from which the class explicitly<br />

inherits (via the keyword extends). An indirect superclass is inherited from two or more<br />

levels up the class hierarchy. For example, class JApplet (package javax.swing)<br />

extends class Applet (package java.applet). Thus, each applet class we have defined<br />

is a direct subclass of JApplet and an indirect subclass of Applet.<br />

With single inheritance, a class is derived from one superclass. <strong>Java</strong> does not support<br />

multiple inheritance (as C++ does) but it does support the notion of interfaces. Interfaces<br />

help <strong>Java</strong> achieve many of the advantages of multiple inheritance without the associated<br />

problems. We will discuss the details of interfaces in this chapter. We consider both general<br />

principles and a detailed specific example of creating and using interfaces.<br />

A subclass normally adds instance variables and instance methods of its own, so a subclass<br />

is generally larger than its superclass. A subclass is more specific than its superclass<br />

and represents a smaller, more specialized group of objects. With single inheritance, the<br />

subclass starts out essentially the same as the superclass. The real strength of inheritance<br />

comes from the ability <strong>to</strong> define in the subclass additions <strong>to</strong>, or replacements for, the features<br />

inherited from the superclass.<br />

Every subclass object is also an object of that class’s superclass. For example, every<br />

applet we have defined is considered <strong>to</strong> be an object of class JApplet. Also, because<br />

JApplet extends Applet, every applet we have defined is considered <strong>to</strong> be an Applet.<br />

This information is critical when developing applets, because an applet container can execute<br />

a program only if it is an Applet. Although a subclass object always can be treated<br />

as one of its superclass types, superclass objects are not considered <strong>to</strong> be objects of their<br />

subclass types. We will take advantage of this “subclass-object-is-a-superclass-object”<br />

relationship <strong>to</strong> perform some powerful manipulations. For example, a drawing application<br />

can maintain a list of shapes <strong>to</strong> display. If all the shape types extend the same superclass<br />

directly or indirectly, the drawing program can s<strong>to</strong>re all the shapes in an array (or other data<br />

structure) of superclass objects. As we will see in this chapter, this ability <strong>to</strong> process a set<br />

of objects as a single type is a key thrust of object-oriented programming.<br />

We add a new form of member access control in this chapter, namely protected<br />

access. Subclass methods and methods of other classes in the same package as the superclass<br />

can access protected superclass members.<br />

Experience in building software systems indicates that significant portions of the code<br />

deal with closely related special cases. It becomes difficult in such systems <strong>to</strong> see the “big picture”<br />

because the designer and the programmer become preoccupied with the special cases.<br />

Object-oriented programming provides several ways of “seeing the forest through the trees.”<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!