Abstract Classes and Interfaces
Abstract Classes and Methods
Suppose we want to write a group of classes representing geometric objects, such a Rectangle, Circle, Triangle, etc. They have certain methods in common, such as area and perimeter (circumference), although these methods have different implementations. The classes have different properties, too. A circle has radius, a rectangle has length and width, a triangle has three side measurements.
For example,
class Circle {
private double radius;
public double area()
{ return PI * radius * radius; }
public double perimeter()
{ return PI * radius * 2; }
}
class Rectangle {
private double length, width;
private double area()
{ return length * width; }
private double perimeter()
{ return 2 * ( length + width ); }
}
Since these classes are similar, it would be convenient to define a common superclass Shape that groups them together in the class hierarchy.
==> We can define an array of Shapes which holds a collection of Circles, Rectangles, etc.
==> We can write a method that accepts a Shape as its argument, say "draw". This method could be passed an object of any shape.
However, creating a Shape class presents some problems:
Q: How can we define the area of a Shape; that is, write its area method?
A: We can't! In this case, Java allows us to declare the area method to be abstract. This means that the class definition for Shape contains only the method's signature (i.e., its name, return type, and parameter list), but no implementation.
Q: If we instantiate a Shape object, and call that object's area method, what will happen?
A: Java can't allow this to happen, because there is no implementation of area for a generic Shape, only for its subclasses.
In this situation, Shape is called an abstract class. An abstract class is used as a common superclass of a group of related classes in the class hierarchy, but cannot itself be instantiated. The only way to create a Shape object is to instantiate a Circle or a Rectangle or a Triangle, etc.
To summarize the rules:
- A class can be declared abstract. An abstract class cannot be instantiated.
- A class with at least one abstract method must be declared abstract.
- A subclass of an abstract class can be instantiated only if it overrides every abstract method of its superclass. (Such a subclass is often called a concrete subclass to emphasize the fact that it is not abstract.)
- If a subclass does not override all the abstract methods of its superclass, it is automatically an abstract class.
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
public class Circle extends Shape {
private double radius;
public double area()
{ return PI * radius * radius; }
public double perimeter()
{ return PI * radius * 2; }
Circle(double radius)
{ this.radius = radius; }
}
A few coding examples to illustrate the use of the abstract class:
example Create an array of Shapes and put some objects in it.
Shape[] shapes = new Shape[5];
shapes[0] = new Circle(1.0);
shapes[1] = new Triangle(2.0, 2.0, 2.0);
shapes[2] = new Circle(2.5);
shapes[3] = new Rectangle(3.0, 4.0);
shapes[4] = new Rectangle(2.0, 2.0);
example Find the sum of the areas of the shapes in the array.
double areasum = 0.0;
for(int i = 0 ; i < 5; i++)
areasum += shapes[i].area();
example Print the area of the shape with the smallest perimeter measurement.
int imin = 0;
for(int i = 1; i < 5; i++)
if( shapes[i].perimeter() < shapes[imin].perimeter() )
imin = i;
System.out.println("The area of the shape with smallest perimeter is " + shapes[imin].area() );