Interfaces & Sealed Classes in Java

From the Java cheat sheet · Inheritance & Interfaces · verified Jul 2026

Interfaces & Sealed Classes

Contracts with default methods and restricted hierarchies.

java
// Quick Reference
interface Shape {
    double area();                 // abstract
    default String label() { return "shape"; } // default method
}
sealed interface Expr permits Num, Add {}
💡 A class implements many interfaces but extends only one class.
⚡ default methods add behavior to an interface without breaking implementers.
📌 sealed + permits locks a hierarchy so switch can be exhaustive with no default.
🟢 Interface methods are public and abstract by default; fields are public static final.
interfacessealed

More Java tasks

Back to the full Java cheat sheet