Java Reference
In-Depth Information
13.1 Introduction
A superclass defines common behavior for related subclasses. An interface can be
used to define common behavior for classes (including unrelated classes).
Key
Point
You can use the java.util.Arrays.sort method to sort an array of numbers or strings.
Can you apply the same sort method to sort an array of geometric objects? In order to write
such code, you have to know about interfaces. An interface is for defining common behavior
for classes (including unrelated classes). Before discussing interfaces, we introduce a closely
related subject: abstract classes.
problem
interface
13.2 Abstract Classes
An abstract class cannot be used to create objects. An abstract class can contain
abstract methods, which are implemented in concrete subclasses.
Key
Point
In the inheritance hierarchy, classes become more specific and concrete with each new sub-
class . If you move from a subclass back up to a superclass, the classes become more general
and less specific. Class design should ensure that a superclass contains common features of its
subclasses. Sometimes a superclass is so abstract that it cannot be used to create any specific
instances. Such a class is referred to as an abstract class .
In Chapter 11, GeometricObject was defined as the superclass for Circle and
Rectangle . GeometricObject models common features of geometric objects. Both
Circle and Rectangle contain the getArea() and getPerimeter() methods for comput-
ing the area and perimeter of a circle and a rectangle. Since you can compute areas and perim-
eters for all geometric objects, it is better to define the getArea() and getPerimeter()
methods in the GeometricObject class. However, these methods cannot be implemented in
the GeometricObject class, because their implementation depends on the specific type of
geometric object. Such methods are referred to as abstract methods and are denoted using the
abstract modifier in the method header. After you define the methods in GeometricObject ,
it becomes an abstract class. Abstract classes are denoted using the abstract modifier in the
class header. In UML graphic notation, the names of abstract classes and their abstract meth-
ods are italicized, as shown in Figure 13.1. Listing 13.1 gives the source code for the new
GeometricObject class.
VideoNote
Abstract GeometricObject
class
abstract class
abstract method
abstract modifier
L ISTING 13.1
GeometricObject.java
1 public abstract class GeometricObject {
2
abstract class
private String color = "white" ;
3
private boolean filled;
4
private java.util.Date dateCreated;
5
6 /** Construct a default geometric object */
7 protected GeometricObject() {
8 dateCreated = new java.util.Date();
9 }
10
11 /** Construct a geometric object with color and filled value */
12 protected GeometricObject(String color, boolean filled) {
13 dateCreated = new java.util.Date();
14
this .color = color;
15
this .filled = filled;
16 }
17
18
/** Return color */
19
public String getColor() {
20
return color;
 
 
 
 
Search WWH ::




Custom Search