Master Java 17 Sealed Classes: Control Inheritance with sealed, permits, non‑sealed
This article explains Java 17’s sealed classes, describing how the new sealed, non‑sealed, and permits keywords let developers precisely restrict inheritance hierarchies, with concrete code examples and a three‑layer hero model illustrating practical usage.
Java 17 introduced sealed classes (JEP 409) after two preview versions (JDK 15 JEP 360, JDK 16 JEP 397). They enable developers to restrict which classes or interfaces may extend or implement a given type.
Purpose of sealed classes
In object‑oriented programming inheritance provides reuse, but sometimes you need to prevent uncontrolled extensions. Sealed classes serve to limit class inheritance.
Existing restriction mechanisms
Prior to sealed classes Java offered two coarse‑grained ways: final – a class cannot be subclassed. package-private – only classes in the same package can extend it.
These mechanisms lack fine‑grained control.
New mechanism: sealed classes
Java 17 adds three important keywords: sealed: declares a class or interface as sealed. non-sealed: declares a class or interface as non‑sealed. permits: used with extends or implements to list the permitted subclasses.
Example: a game with heroes divided into three categories—Tank, Attack, Support—each with concrete subclasses.
public class Hero { }
public class TankHero extends Hero { }
public class AttackHero extends Hero { }
public class SupportHero extends Hero { }
public class Alistar extends TankHero { }
public class Ezreal extends AttackHero { }
public class Soraka extends SupportHero { }The hierarchy has three layers: a base Hero, three abstract category classes, and concrete hero implementations.
To protect the first two layers, declare the base class as sealed and list the allowed subclasses:
public sealed class Hero permits TankHero, AttackHero, SupportHero { }If a subclass remains sealed, it must also specify its permitted subclasses; otherwise it must be declared non-sealed or final. Attempting to compile the previous concrete classes without such modifiers yields the error “sealed, non-sealed or final modifiers expected”.
For the second‑layer abstract classes we can use non-sealed so that further concrete heroes may be added:
public non-sealed class TankHero extends Hero { }Concrete hero classes can be declared final to prevent further inheritance:
public final class Ezreal extends AttackHero { }With this arrangement the first two layers are firmly protected while the third layer remains extensible.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
