Java: Keyword “abstract”

By Xah Lee. Date: . Last updated: .

The “abstract” Keyword

Purpose of Abstract Classes

The purpose of abstract class is to serve purely as a parent of classes. (that is, solely for the purpose of extension (inheritance) by other classes.)

The difference between abstract class and “normal” class is that, the abstract class's methods cannot have any definition.

Syntax and Code Example

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:

// declare 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:

// declare a abstract method
abstract int get_person_id (String name);

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

Examples of Abstract Class

For example, the java.lang.Number is a abstract class.

java.lang.Number's abstract methods includes

The following classes: {Byte, Double, Float, Integer, Long, Short} are children of java.lang.Number, and implement those methods, each differently.

It does not make sense to have a default implementation in java.lang.Number for the methods, because each is really specific to the type.

Abstract Class Summary

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

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 require the child class to have the methods implemented.

Java

Java Basics

Class, Inheritance

Abstract class, Interface

Package

Misc