19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

562 Chapter 15 Abstract Classes and Interfaces<br />

abstract method<br />

abstract method<br />

why protected construc<strong>to</strong>r?<br />

implementing Circle<br />

implementing Rectangle<br />

extends abstract<br />

GeometricObject<br />

extends abstract<br />

GeometricObject<br />

41 return dateCreated;<br />

42 }<br />

43<br />

44 @Override<br />

45 public String <strong>to</strong>String() {<br />

46 return "created on " + dateCreated + "\ncolor: " + color +<br />

47 " and filled: " + filled;<br />

48 }<br />

49<br />

50 /** Abstract method getArea */<br />

51 public abstract double getArea();<br />

52<br />

53 /** Abstract method getPerimeter */<br />

54 public abstract double getPerimeter();<br />

55 }<br />

Abstract classes are like regular classes, but you cannot create instances of abstract classes<br />

using the new opera<strong>to</strong>r. An abstract method is defined without implementation. Its implementation<br />

is provided by the subclasses. A class that contains abstract methods must be<br />

defined as abstract.<br />

The construc<strong>to</strong>r in the abstract class is defined as protected, because it is used only by subclasses.<br />

When you create an instance of a concrete subclass, its superclass’s construc<strong>to</strong>r is<br />

invoked <strong>to</strong> initialize data fields defined in the superclass.<br />

The GeometricObject abstract class defines the <strong>com</strong>mon features (data and methods)<br />

for geometric objects and provides appropriate construc<strong>to</strong>rs. Because you don’t know how <strong>to</strong><br />

<strong>com</strong>pute areas and perimeters of geometric objects, getArea and getPerimeter are<br />

defined as abstract methods. These methods are implemented in the subclasses. The<br />

implementation of Circle and Rectangle is the same as in Listings 15.2 and 15.3, except<br />

that they extend the GeometricObject class defined in this chapter.<br />

LISTING 15.2<br />

Circle.java<br />

1 public class Circle extends GeometricObject {<br />

2 // Same as lines 3-48 in Listing 11.2, so omitted<br />

3 }<br />

LISTING 15.3<br />

Rectangle.java<br />

1 public class Rectangle extends GeometricObject {<br />

2 // Same as lines 3-51 in Listing 11.3, so omitted<br />

3 }<br />

15.2.1 Why Abstract Methods?<br />

You may be wondering what advantage is gained by defining the methods getArea and<br />

getPerimeter as abstract in the GeometricObject class. The example in Listing 15.4<br />

shows the benefits of defining them in the GeometricObject class. The program creates two<br />

geometric objects, a circle and a rectangle, invokes the equalArea method <strong>to</strong> check whether<br />

they have equal areas, and invokes the displayGeometricObject method <strong>to</strong> display them.<br />

create a circle<br />

create a rectangle<br />

LISTING 15.4<br />

TestGeometricObject.java<br />

1 public class TestGeometricObject {<br />

2 /** Main method */<br />

3 public static void main(String[] args) {<br />

4 // Create two geometric objects<br />

5 GeometricObject geoObject1 = new Circle(5);<br />

6 GeometricObject geoObject2 = new Rectangle(5, 3);

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

Saved successfully!

Ooh no, something went wrong!