Language syntax

Java Abstract class

Defines a class that cannot be directly instantiated, but may contain state, constructors and concrete methods.

Formal Syntax & Structure

java syntaxjava
abstract class Delivery

Working Example

Example Snippetjava
abstract class Delivery {    private final int items;    Delivery(int items) {        if (items < 0 || items > 100) throw new IllegalArgumentException("items");        this.items = items;    }    protected int items() { return items; }    public abstract int fee();}class Pickup extends Delivery {    Pickup(int items) { super(items); }    public int fee() { return 0; }}class Courier extends Delivery {    Courier(int items) { super(items); }    public int fee() { return items() == 0 ? 0 : 100 + 20 * items(); }}public class Main {    public static void main(String[] args) {        Delivery[] orders = {new Pickup(3), new Courier(3), new Courier(0)};        for (Delivery order : orders) System.out.println(order.fee());    }}
Java Reference