Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Writing Abstract Classes and Methods

Abstract Classes

Sometimes, a class that you define represents an abstract concept and as such, it doesn't make sense for objects of that type to exist. Take, for example, food in the real world. Have you ever seen an instance of food? No. What you see instead are instances of carrot, apple, and chocolate. Food represents the abstract concept of things that we can eat. It doesn't make sense for an instance of food to exist.

Similarly in object-oriented programming, you may want to model abstract concepts but you don't want to be able to create an instance of the concept. For example, the Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to classes like Integer and Float which implement specific cases of numbers. Classes such as Number, which implement abstract concepts and should not be instantiated, are called abstract classes. An abstract class is a class that can only be subclassed--it cannot be instantiated.

To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration:

abstract class Number {
    . . .
}
If you attempt to instantiate an abstract class, the compiler will display an error similar to the following and refuse to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class. It can't be instantiated.
	new AbstractTest();
	^
1 error

Abstract Methods

An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define its complete programming interface and thus provide its subclasses with the method declarations for all of the methods necessary to implement that programming interface. However, the abstract class can leave the implementation details of those methods up to its subclasses.

Let's look at an exmaple of when you might want to create an abstract class with an abstract method in it. In an object-oriented drawing application, you can draw circles, rectangles, lines, beziers, and so on. Each of these graphic objects share certain attributes (position, bounding box) and behaviours (move, resize, draw), so it makes sense for them all to inherit from the same parent object--GraphicObject. However, the graphic objects are also substantially different in many ways: drawing a circle is quite different than drawing a rectangle. It does not make sense for the graphics objects to share these types of attributes or behaviours. On the other hand, all GraphicObject's must know how to draw themselves; they just differ in how they are drawn. This is a perfect situation for an abstract superclass.

First you would declare an abstract class, GraphicObject, that provided all of the member variables and methods that were wholly shared by all of the subclasses. GraphicObject would also declare abstract methods for those methods, such as draw(), that needed to be implemented by all subclasses, but implemented in entirely different ways (no suitable default implementation in the superclass made sense). The GraphicObject class would look something like this:

abstract class GraphicObject {
    int x, y;
    . . .
    void moveTo(int newX, int newY) {
        . . .
    }
    abstract void draw();
}
Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, would have to provide an implementation for the draw() method.
class Circle extends GraphicObject {
    void draw() {
        . . .
    }
}
class Rectangle extends GraphicObject {
    void draw() {
        . . .
    }
}
An abstract class is not required to have an abstract method in it. But any class that does have an abstract method in it or in any of its superclasses must be declared as an abstract class.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces