Lesson content
Read, practise, then check your understanding
An abstract class cannot be instantiated and can mix fields, constructors, concrete methods, and abstract methods. An interface primarily specifies a role; a class may implement many interfaces.
Contracts
interface Identified {
String id();
default boolean hasId() { return !id().isBlank(); }
}
abstract class Entity implements Identified {
private final String id;
protected Entity(String id) { this.id = id; }
@Override public final String id() { return id; }
}
Interface fields are implicitly public static final; abstract interface methods are public. Default methods evolve interfaces but conflicting inherited defaults must be resolved explicitly. Private interface methods share implementation internally. Choose an abstract class for shared state and controlled construction, an interface for an independently implementable capability, and composition for pluggable behavior. Sealed classes/interfaces list permitted direct subtypes, enabling controlled domain models and more exhaustive pattern handling.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.