Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The abstract keyword

Reference for the Java abstract keyword: abstract classes that cannot be instantiated, abstract methods without a body, and how subclasses must complete them.

abstract marks something as incomplete on purpose. An abstract class cannot be instantiated with new; it exists to be extended. An abstract method has a signature but no body, and every concrete subclass must implement it.

Abstract classes are useful when several related classes share state and code but each must supply a few steps of its own. A shape class can hold a name and a describe() method while leaving area() abstract.

Java
abstract class Shape {
    abstract double area();             // no body
    String describe() { return "area " + area(); }
}

Try it

Java An abstract base class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Template method and an anonymous subclass

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
[access] abstract class Name { }
[access] abstract ReturnType method(params);   // ends with ;, no body
Rule Detail
Instantiation new on an abstract class is a compile error, except in an anonymous subclass that implements everything.
Abstract methods Allowed only in abstract classes (and interfaces). No body, no braces.
Concrete subclass Must implement all inherited abstract methods, or be declared abstract itself.
A class may be abstract with no abstract methods This only prevents instantiation.
Constructors Allowed. They run when a subclass is created, through super(...).

Usage notes

  • State and code: unlike interfaces, abstract classes can have instance fields, constructors and non-public members. A class can extend only one of them.
  • Illegal combinations: abstract cannot be combined with final, private or static on a method, because each makes overriding impossible. abstract final class is also rejected.
  • Constructor trap: calling an abstract method from the abstract class's constructor runs the subclass version before the subclass's fields are initialized. Avoid it.
  • Abstract vs interface: use an interface to define a capability that unrelated classes can have. Use an abstract class when subclasses share state or a partial implementation. Interfaces with default methods blur the line, but still cannot hold instance fields.
  • Reference type: you can declare variables of an abstract type; they hold instances of concrete subclasses.
  • Sealed hierarchies: an abstract sealed class lists the only allowed subclasses; see sealed.
  • Enums and records: an enum constant can supply a body that implements an abstract method of the enum. Records cannot be abstract.