3 · Object-Oriented Core · lesson 18 of 41
Interfaces, Default & Sealed
Contracts with optional bodies, and restricted hierarchies.
Java
interface Drawable {
void draw();
default void redraw() { // Java 8+ default method
clear(); draw();
}
private void clear() { /* ... */ }
}
// Sealed (Java 17+) — only listed types may implement.
sealed interface Shape permits Circle, Square {}
final class Circle implements Shape {}
final class Square implements Shape {}Key takeaways
- ✓Interfaces are pure contracts; a class can implement many.
- ✓default methods add behavior without breaking implementers.
- ✓sealed types make pattern matching exhaustive.