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

Class Circle (Fig. 9.24) is derived from Point. A Circle has a volume of 0.0, so<br />

superclass method volume is not overridden—it is inherited from class Point, which<br />

inherited it from Shape. A Circle has an area different from that of a Point, so the<br />

area method is overridden. Method getName is an implementation of the abstract<br />

method in the superclass. If this method is not overridden here, the Point version of get-<br />

Name would be inherited. Other methods include setRadius <strong>to</strong> assign a new radius<br />

<strong>to</strong> a Circle and getRadius <strong>to</strong> return the radius of a Circle.<br />

1 // Fig. 9.24: Circle.java<br />

2 // Definition of class Circle<br />

3<br />

4 public class Circle extends Point { // inherits from Point<br />

5 protected double radius;<br />

6<br />

7 // no-argument construc<strong>to</strong>r<br />

8 public Circle()<br />

9 {<br />

10 // implicit call <strong>to</strong> superclass construc<strong>to</strong>r here<br />

11 setRadius( 0 );<br />

12 }<br />

13<br />

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

15 public Circle( double circleRadius, int xCoordinate,<br />

16 int yCoordinate )<br />

17 {<br />

18 // call superclass construc<strong>to</strong>r<br />

19 super( xCoordinate, yCoordinate );<br />

20<br />

21 setRadius( circleRadius );<br />

22 }<br />

23<br />

24 // set radius of Circle<br />

25 public void setRadius( double circleRadius )<br />

26 {<br />

27 radius = ( circleRadius >= 0 ? circleRadius : 0 );<br />

28 }<br />

29<br />

30 // get radius of Circle<br />

31 public double getRadius()<br />

32 {<br />

33 return radius;<br />

34 }<br />

35<br />

36 // calculate area of Circle<br />

37 public double area()<br />

38 {<br />

39 return Math.PI * radius * radius;<br />

40 }<br />

41<br />

Fig. Fig. Fig. 9.24 9.24 9.24 Circle subclass of Point—indirect subclass of abstract class<br />

Shape (part 1 of 2).<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!