The “abstract” Keyword In Java

Xah Lee, 2005-02-21

Previously we've covered the “static” keyword, which can be used for variable declaration and method declaration as to make them class variables or class methods. (as opposed to being instance variables and methods. See Java's Instance Variables vs Class Variables )

Note that the “static” keyword cannot be used in front of a class. (try it) However, a somewhat similar idea is abstract classes with the “abstract” keyword.

The “abstract” keyword can be used on classes and methods. A class declared with the “abstract” keyword cannot be instantiated, and that is the only thing the “abstract” keyword does. Example of declaring a abstract class:

abstract CalendarSystem (String name);

When a class is declared abstract, then, its methods may also be declared abstract.

When a method is declared abstract, the method can not have a definition. This is the only effect the abstract keyword has on method. Here's a example of a abstract method:

abstract int get_person_id (String name);

Abstract classes and abstract methods are like skeletons. It defines a structure, without any implementation.

Note that, only abstract classes can have abstract methods. Abstract class does not necessarily require its methods to be all abstract.

Classes declared with the abstract keyword are solely for the purpose of extension (inheritance) by other classes.

The following is a code template to experiment with. Begin by commenting out lines involving H2. Experiment with just defining a abstract class. See what is allowed, what is not. Then, define H2 that extends a abstract class. Get a feel of what needs to be there or not. Or, what kind of complaints compiler makes. Finally, create a object of a class that is inherited from a abstract class.

// a abstract class
abstract class H {
    int x;

    /* abstract */ int y; // variables cannot be abstract

    /* abstract */ H () {x=1;} // constructer cannot be abstract

    void triple  (int n) {x=x*3;}; // a normal method

    static int triple2 (int n) {return n*3;}; // a static method in abstract class is ok.
    abstract void triple3 (); // abstract method. Note: no definition.

    // static abstract void triple3 (int n); // abstract static cannot be combined
    int returnMe () {return x;}
}

// H1 extends (inherits) H. When a class extends a abstract class,
// it is said to “implement” the abstract class.
class H1 extends H {
    void triple3 () {x=x*3+1;} // must be defined, else compiler makes a complaint.
    //Also, all return type and parameter must agree with the parent class.
}


public class abbst {
    public static void main(String[] args) {
        // H xx = new H(); // abstract class cannot be instantiated
        H1 myO = new H1(); // abstract class cannot be instantiated
        myO.triple3();
        System.out.println(myO.returnMe());
    }
}

The Purpose Of Abstract Classes

Abstract class and methods are complications arising out of the pure Object Oriented Paradigm.

Its purpose is to serve purely as a umbrella of classes.

That is to say: abstract class is a parent for a collection of sub classes, where itself declares certain methods but doesn't define any implementations. This is so that, sub classes all inherit the same set of methods and variables, and are free to implement them or add in addition.

For example, the “java.lang.Number” is a abstract class. Its sub classes includes:

Byte, Double, Float, Integer, Long, Short

All these subclass inherit and implement “java.lang.Number”'s abstract methods:

byteValue()
doubleValue()
floatValue()
intValue()
longValue()
shortValue()

It does not make sense to give them a implementation in “java.lang.Number”, because each is really specific to the type. For example, the implementation for “Double.intValue()” is necessarily different for “Integer.intValue()”.

Reference: Java Doc: Number↗.

So what is special about abstract classe? after all, a normal class can have subclasses just as well, and function as a umbrella to other classes. The key to abstract class is that it allows methods to be undefined. It explicitly tells the programers, that “I'm a embodiment of classes. I define what methods my children must bear, while I, do not have to subject myself to the precepts of OOP machinery of providing implementation to methods. I'm exceptional. I am a embodiment of classes.”

You could define “java.lang.Number” as a normal class, with implementation of the intValue() and other methods, and then redefine the method in one of the subclass such as Double. However, that would be silly since your “java.lang.Number.intValue()” is meant to be over-written anyway, and it does not make any sense to give it a default implementation because the implementation depends on the type.

Again: In Java, the concept of abstract class and the keyword “abstract”, is to serve purely as a umbrella for other classes. Remember this, because it can get confusing later with another complexity out of OOP: Java Interfaces.

Quiz: can a normal class have abstract methods?

For a juvenile drivel about abstract classes using metaphors of food and chocolate chip cookies, see the official Java Tutorial: java_tutorial_20051223_abstract.zip. In a version dated 20060803, the food analogy is removed, in fact the entire explanatory section is removed. Instead it takes a pure technical approach, and dragging in interfaces and other intricacies, making it more confusing. java_tutorial_20060803_abstract.zip.

Q and A with Abstract Class

What is the purpose of abstract classes?

Its purpose is to serve purely as a umbrella of classes.

As a parent, what's the difference between abstract class and normal class?

In a normal class, methods need to have definitions. If your class is to be a umbrella of other classes, it sometimes does not make sense to give a default definition for methods. For example, if your umbrella class is Number, and have Integer and ComplexNumber as children, it doesn't make sense to have a default definition for a method Plus.

Another example: If you have a GraphicObject as parent, and Rectangle and Circle as children. It does not make sense, to have DrawIt predefined in GraphicObject.

When a class is declared abstract, it needs not to have definitions for its methods. Therefore, it is ideal, for serving as pure parent of classes.

Viewed alternatively, Java's class hierachy system has a problem, in some cases, because of its required inherence of method definitions. A programer's workaronud is to give a dummy definition in the parent class. (such as returning nothing). Abstract class is the official solution at the language level.

What is the difference between abstract class and interface?

Their similarity, is that both interface and abstract class require the child class to have the methods set.

However, they serve difference purposes. A interface is a way to declare that the class have a set of methods. While, abstract class is a way to declare a hiarachical relationship among classes.

Reference: http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html


See also:


Page created: 2005-02.
© 2005 by Xah Lee.
Xah Signet