Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 Old CS 160 Abstract Classes and Interfaces
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Abstract Classes and Interfaces

by admin last modified 2005-05-25 15:41

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:
  1. A class can be declared abstract.  An abstract class cannot be instantiated.
  2. A class with at least one abstract method must be declared abstract.
  3. 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.)
  4. If a subclass does not override all the abstract methods of its superclass, it is automatically an abstract class.
In our Shapes example, the Shape and Circle classes would be defined as follows:

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() );



 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: