The “abstract” Keyword in Java

, ,

Previously we've covered the “static” keyword. It is used to declare variables and methods. Once declared “static”, they are called “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

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.

Code Template to Experiment with “abstract” Keyword

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;} // constructor 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 parent of classes.

Its only effect is to prevent you from giving a definition, of the following cases:

Rational for Abstract Class/Method

Abstract class is a parent for a collection of subclasses, where itself declares certain methods but doesn't define any implementations. This is so that, subclasses 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 subclasses 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().

Java Doc: Number

Abstract Class vs Normal Class

So what is special about abstract classe? after all, a normal class can have subclasses just as well, and can be parent 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 because 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 parent 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 2006-08-03, 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.

Abstract Class Summary

What is the purpose of abstract classes?

Its purpose is to serve purely as a parent 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 parent of other classes, it sometimes does not make sense to give a default definition for methods. For example, if your parent 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 hierarchy system has a problem, in some cases, because of its required inherence of method definitions. A programer's workaround is to give a dummy definition in the parent class. (such as do nothing). Abstract class is the official solution at the language level.

What is the difference between abstract class and interface?

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

abstract class and interface are completely different. 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.

For example:

see also https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

blog comments powered by Disqus