Top 10 Sealed Class Rules Every Java 21 Certification Taker Must Know
Read this MyExamCloud Blog article for practical insights on Java Certification. Explore more blog categories, search related topics in blog search, or return to the MyExamCloud Blog home.
Java 21 introduced sealed classes to provide better control over class hierarchies. If you're preparing for the Java 21 Developer Certification, understanding these rules is crucial. This guide breaks down the top 10 rules you must know to ace your exam. Plus, we recommend MyExamCloud Java 21 Developer Certification Practice Tests to solidify your understanding.
1. Restricting Inheritance
Sealed classes prevent arbitrary inheritance by specifying a fixed set of allowed subclasses. This ensures a well-defined class hierarchy.
Example:
public sealed class Shape permits Circle, Square, Rectangle { }
2. Declaring a Sealed Class
A class becomes sealed when declared with the sealed keyword, followed by a permits clause listing its allowed subclasses.
Example:
public sealed class Shape permits Circle, Square, Rectangle { }
3. Permitted Subclasses Must Be Explicitly Listed
Subclasses must be explicitly mentioned in the permits clause, ensuring that only specified classes can extend the sealed class.
Example:
public sealed class Shape permits Circle, Square, Rectangle { }
4. Permitted Subclasses Must Use One of Three Modifiers
Each permitted subclass must be declared using one of these modifiers:
final→ Cannot be extended further.sealed→ Can only be extended by specified subclasses.non-sealed→ Can be extended by any class.
Example:
public final class Circle extends Shape { }
public sealed class Rectangle extends Shape permits FilledRectangle { }
public non-sealed class Square extends Shape { }
5. Alternative Declaration Without permits Clause
If permitted subclasses are defined in the same file as the sealed class, the permits clause can be omitted.
Example:
public sealed class Figure { }
final class Circle extends Figure { }
non-sealed class Square extends Figure { }
sealed class Rectangle extends Figure { }
6. Permitted Subclasses Must Be in the Same Module or Package
- If the sealed class is in a named module, its subclasses must be in the same module.
- If the sealed class is in an unnamed module, its subclasses must be in the same package.
Example (valid only in the same module):
package com.example.graphics;
public sealed class Shape
permits com.example.polar.Circle,
com.example.quad.Rectangle,
com.example.quad.simple.Square { }
7. Sealed Interfaces Follow Similar Rules
Sealed interfaces restrict which classes or interfaces can implement them.
Example:
sealed interface Expr permits ConstantExpr, PlusExpr, TimesExpr, NegExpr {
int eval();
}
final class ConstantExpr implements Expr {
int i;
ConstantExpr(int i) { this.i = i; }
public int eval() { return i; }
}
8. Record Classes Can Be Permitted Subclasses
Record classes can be used as permitted subclasses in sealed classes or interfaces since records are implicitly final.
Example:
sealed interface Expr permits ConstantExpr, PlusExpr, TimesExpr, NegExpr { }
record ConstantExpr(int i) implements Expr {
public int eval() { return i(); }
}
9. Narrowing Reference Conversion Checks
The compiler ensures that illegal type casts do not occur between disjoint sealed types.
Example (invalid cast):
public sealed interface Shape permits Polygon { }
public non-sealed interface Polygon extends Shape { }
public final class UtahTeapot { }
public void work(Shape s) {
UtahTeapot u = (UtahTeapot) s; // Error: Shape and UtahTeapot are disjoint
}
10. API Support for Sealed Classes
Java provides built-in methods to inspect sealed classes at runtime:
Class.permittedSubclasses(): Returns an array of permitted subclasses.Class.isSealed(): Checks if a class is sealed.
Example:
Class<?> clazz = Shape.class;
System.out.println(clazz.isSealed()); // true
Ace Your Java 21 Certification with MyExamCloud
Understanding sealed classes is just one part of mastering Java 21. To ensure you're fully prepared, we recommend MyExamCloud Java 21 Developer Certification Practice Tests. Their practice tests and study materials cover all the latest topics, helping you confidently pass your certification exam.
Start practicing today and take your Java skills to the next level!
By mastering these sealed class rules, you'll be well-equipped to tackle any related questions in the Java 21 certification exam. Happy coding and best of luck!
| Author | Ganesh P Certified Artificial Intelligence Scientist (CAIS) | |
| Published | 1 year ago | |
| Category: | Java Certification | |
| HashTags | #Java #Programming #JavaCertification |

