Adult web hosting - Chapter 9 Object-Oriented Programming 519 1. If a
Tuesday, October 16th, 2007Chapter 9 Object-Oriented Programming 519 1. If a class A is a subclass of class B, then A extends B in the class declaration and calls the constructor of B. For example, class Elevator is a subclass of abstract superclass Location, so the class declaration should read public class Elevator extends Location { public Elevator { super(); } … 2. If class B is an abstract class and class Ais a subclass of class B, then class Amust override the abstract methods of class B (if class A is to be a concrete class). For example, class Location contains abstract methods getLocationName, getButton and getDoor, so class Elevator must override these methods (note that getButton returns the Elevator s Button object, and getDoor returns the Elevator s Door object Elevator contains associations with both objects, according to the class diagram of Fig. 9.38). public class Elevator extends Location { // class attributes private boolean moving; private boolean summoned; private Location currentFloor; private Location destinationFloor; private int capacity = 1; private int travelTime = 5; // class objects private Button elevatorButton; private Door elevatorDoor; private Bell bell; // class constructor public Elevator() { super(); } // class methods public void ride() {} public void requestElevator() {} public void enterElevator() {} public void exitElevator() {} public void departElevator() {} // method overriding getLocationName public String getLocationName() { return “elevator”; } Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01