oop
13. Sealed Classes: Controlled Inheritance
Analyze the code, write your predicted output, and then reveal the solution.
Scenario
Java 17 introduced Sealed Classes to allow developers to explicitly state which classes are allowed to extend a class.
Code Block
// Only Circle and Square are allowed to extend Shape sealed class Shape permits Circle, Square { abstract void draw(); } final class Circle extends Shape { void draw() { System.out.println("Drawing Circle"); } } non-sealed class Square extends Shape { void draw() { System.out.println("Drawing Square"); } } // class Triangle extends Shape { } // COMPILE ERROR: Triangle is not permitted public class Main { public static void main(String[] args) { Shape s = new Circle(); s.draw(); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.