Core Java: A sealed class permits a non-sealed subclass to reopen the hierarchy

A subclass listed as non-sealed in a sealed interface's permits clause is unrestricted, so anyone can extend it further, breaking the closed-hierarchy guarantee at that one branch.

Code
sealed interface Shape permits Circle, Square {}
record Circle(double r) implements Shape {}
non-sealed class Square implements Shape {
    double side;
    Square(double side) { this.side = side; }
}
class Rectangle extends Square {
    Rectangle(double side) { super(side); }
}
Shape s = new Rectangle(4);
System.out.println("s is a Square subtype: " + (s instanceof Square));
System.out.println("s is a Rectangle: " + (s instanceof Rectangle));
Output
s is a Square subtype: true
s is a Rectangle: true
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms