Java Reference
In-Depth Information
// Constructs a new colored point with the given
// coordinates and color.
public ColoredPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
// Returns this point's color.
public Color getColor() {
return color;
}
}
23.
// A general interface for shape classes.
public interface Shape {
public double getArea();
public double getPerimeter();
public int getSideCount();
}
Here are the implementations of the method in the
Circle
,
Rectangle
, and
Triangle
classes:
// Returns the number of sides a circle has (0).
public int getSideCount() {
return 0;
}
// Returns the number of sides a rectangle has (4).
public int getSideCount() {
return 4;
}
// Returns the number of sides a triangle has (3).
public int getSideCount() {
return 3;
}
24.
An abstract class is intended to be used only as a superclass for inheritance. It's like a
normal class in that it can have fields, methods, constructors, and so on. It's different
from a normal class in that it can have abstract methods, which are like methods of an
interface because only their headers are given, not their bodies. It's also different from
a normal class because it can't be instantiated (used to create objects).
25.
One good design would be to have an abstract superclass named
Movie
with data
such as
name
,
director
, and
date
. Subclasses of
Movie
would represent particu-
lar movie types, such as
Drama
,
Comedy
, and
Documentary
. Each subclass would
store its specific data and behavior.
Search WWH ::
Custom Search