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 519<br />

1. If a class A is a subclass of class B, then A extends B in the class declaration and<br />

calls the construc<strong>to</strong>r of B. For example, class Eleva<strong>to</strong>r is a subclass of abstract<br />

superclass Location, so the class declaration should read<br />

public class Eleva<strong>to</strong>r extends Location {<br />

public Eleva<strong>to</strong>r<br />

{<br />

super();<br />

}<br />

...<br />

2. If class B is an abstract class and class A is a subclass of class B, then class A must<br />

override the abstract methods of class B (if class A is <strong>to</strong> be a concrete class). For<br />

example, class Location contains abstract methods getLocationName,<br />

getBut<strong>to</strong>n and getDoor, so class Eleva<strong>to</strong>r must override these methods<br />

(note that getBut<strong>to</strong>n returns the Eleva<strong>to</strong>r’s But<strong>to</strong>n object, and getDoor<br />

returns the Eleva<strong>to</strong>r’s Door object—Eleva<strong>to</strong>r contains associations with<br />

both objects, according <strong>to</strong> the class diagram of Fig. 9.38).<br />

public class Eleva<strong>to</strong>r extends Location {<br />

// class attributes<br />

private boolean moving;<br />

private boolean summoned;<br />

private Location currentFloor;<br />

private Location destinationFloor;<br />

private int capacity = 1;<br />

private int travelTime = 5;<br />

// class objects<br />

private But<strong>to</strong>n eleva<strong>to</strong>rBut<strong>to</strong>n;<br />

private Door eleva<strong>to</strong>rDoor;<br />

private Bell bell;<br />

// class construc<strong>to</strong>r<br />

public Eleva<strong>to</strong>r()<br />

{<br />

super();<br />

}<br />

// class methods<br />

public void ride() {}<br />

public void requestEleva<strong>to</strong>r() {}<br />

public void enterEleva<strong>to</strong>r() {}<br />

public void exitEleva<strong>to</strong>r() {}<br />

public void departEleva<strong>to</strong>r() {}<br />

// method overriding getLocationName<br />

public String getLocationName()<br />

{<br />

return "eleva<strong>to</strong>r";<br />

}<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!