Abstract classes and interfaces
What you will learn
An abstract class can define shared state and operations while leaving some behavior to subclasses. An interface describes a type that different classes can implement. Both allow clients to work with a common contract without depending on every implementation detail.
After this lesson you can:
- Implement a concrete subclass of an abstract class.
- Use a shared interface type for unrelated implementations.
Use a separate Main.java for each example. Compile with javac Main.java and run with java Main. The code is compatible with Java 21; Java 25 execution is not yet verified.
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()); }}Expected output:
01600All three objects honor the fee contract, while the implementation differs by delivery type.
Choose a common contract
Delivery stores a validated item count and declares fee() without a body. A concrete subclass must provide the missing behavior (or inherit a suitable implementation); it cannot leave that obligation unresolved. You cannot construct Delivery directly, but its constructor runs when a Pickup or Courier is created. Abstract classes may contain concrete methods and fields, and can even contain no abstract methods.
An interface such as Label declares text() with no body. In this example the method is implicitly public and abstract; an implementation must make it public. A class can extend one direct superclass and implement multiple interfaces. An interface can extend multiple interfaces. Interface fields are constants in the sense that they are public static final fields; they are not per-object state, and a final reference may still refer to mutable data. Interfaces have no constructors. Default, static and private methods are covered in the last lesson of this chapter.
Choose a superclass when related implementations genuinely need its state or shared lifecycle; choose an interface for a capability that may be shared across unrelated classes. Neither choice guarantees correct behavior: document the valid input, output and failure contract, and test each implementation against it. Composition remains an option when a class merely needs another object's services.
interface Label { String text();}class ShelfLabel implements Label { public String text() { return "Shelf A"; }}class BoxLabel implements Label { public String text() { return "Box 3"; }}public class Main { public static void main(String[] args) { Label[] labels = {new ShelfLabel(), new BoxLabel()}; for (Label label : labels) System.out.println(label.text()); }}Expected output:
Shelf ABox 3The loop depends on Label rather than either concrete class.
Check a boundary or variation
interface Printable { String printText(); }interface Identified { int id(); }class Pass implements Printable, Identified { public int id() { return 7; } public String printText() { return "Pass " + id(); }}public class Main { public static void main(String[] args) { Pass pass = new Pass(); Printable printable = pass; Identified identified = pass; System.out.println(printable.printText()); System.out.println(identified.id()); }}Expected output:
Pass 77Both references designate the same Pass object; each exposes the operations of its declared interface.
Common mistakes
Do not instantiate an abstract class or interface directly, omit an interface method implementation in a concrete class, or use package access for a method that implements a public interface operation.
Practice
Create Score with int points(). Completed takes a task count from 0 to 20 and returns 10 points per task; Bonus always returns 5. For this task callers supply valid task counts. Sum points through a Score[] for mixed, zero-task and empty cases.
Declare public points() methods in both classes. Start the total at zero and call points() on each Score in the loop.
Next: Overriding and dynamic dispatch.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Abstract classes and interfaces — knowledge check
Apply the stated Java 21-compatible contracts and predict observable behavior.
Can an abstract class contain a constructor?
What does a concrete subclass need to do with an inherited abstract method?
How many direct interfaces can a class implement?
What access must ShelfLabel.text() have?
Which type should a loop use if it needs only Label.text()?