Nearby lessons

102 of 125

Java - Sealed Classes

📌 What You Will Learn
  • What happened in Java after version 14
  • The new LTS versions: 17 and 21
  • Records and sealed classes — modern class design
  • Virtual threads — the future of concurrency
  • A practical roadmap to stay current

Sealed Classes is a core concept of the Java language. This lesson explains Java 17 — Sealed Classes with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Java 17 — Sealed Classes

A sealed class controls exactly which classes may extend it. It sits between open to everyone (normal) and closed to everyone (final):

Why useful? It makes a closed family of types, so switch statements can cover all cases safely (the compiler knows the full list).

In simple words: A sealed class is a family of classes with a fixed member list. Only the classes named in permits can extend it, so the compiler always knows every possible subclass.
Example01
JCode Cell
1sealed class Shape permits Circle, Square, Triangle { }
2final class Circle extends Shape { }
3final class Square extends Shape { }
4final class Triangle extends Shape { }
5 
6// any other class trying to extend Shape -> compile error
📝 Key Takeaways
  • Modern Java is released every 6 months; LTS versions are 8, 11, 17, 21.
  • Text blocks and records make code shorter and cleaner.
  • Sealed classes control exactly who can extend a class.
  • Pattern matching for switch works on types and null.
  • Virtual threads (Java 21) let servers handle millions of tasks.
  • Previews (templates, structured concurrency) are the future — learn the finalized features first.
  • Java 8 concepts remain the everyday foundation in every job.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4