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.
abstract class Shape { abstract double area(); // no body String describe() { return "area " + area(); } }
Try it
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.
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
[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:
abstractcannot be combined withfinal,privateorstaticon a method, because each makes overriding impossible.abstract final classis 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
sealedclass lists the only allowed subclasses; seesealed. - Enums and records: an enum constant can supply a body that implements an abstract method of the enum. Records cannot be abstract.