Java Sealed Class

Sealed classes, introduced in Java 15 as a preview feature and later refined in subsequent releases, represent a middle ground between the two extremes of the class world: the completely open public class, and the completely closed final class.

What is a Sealed Class?

A sealed class restricts which other classes or interfaces can extend or implement it. It’s defined using the sealed, non-sealed, or permits modifier.

Basic Syntax:

public sealed class Shape 
    permits Circle, Rectangle, Triangle { }

In this example, Shape is a sealed class, and only Circle, Rectangle, and Triangle can extend it.

Using Sealed Classes:

Defining:

public sealed class Animal 
    permits Dog, Cat { }

public final class Dog extends Animal { }

public final class Cat extends Animal { }

Note: Classes extending a sealed class can be final, sealed, or non-sealed.

Benefits (Advantages):

  1. Controlled Hierarchies: You have greater control over how your class is used and extended. It’s particularly useful when you want to restrict subclasses for a specific domain problem.
  2. Pattern Matching: In conjunction with Java’s pattern matching features, sealed classes offer more robust compile-time checks. The compiler can verify if all permitted subclasses are covered in a pattern-matching block, allowing for exhaustive pattern matching.
  3. Maintainability: By restricting which classes can extend your class, you can avoid potential future complications when your codebase evolves or is refactored.

Drawbacks (Disadvantages):

  1. Flexibility: Sealed classes can reduce the flexibility for developers, especially if they want to extend a class in a way that wasn’t anticipated by the original developer.
  2. Complexity: For smaller projects or cases where exhaustive control isn’t needed, using sealed classes might introduce unnecessary complexity.

When to Use:

  1. Domain Modeling: When modeling a specific domain where you know all the possible subclasses, sealed classes can be beneficial. For example, modeling geometric shapes, AST nodes in compilers, or specific kinds of events in an event system.
  2. API Design: If you’re designing an API and want to ensure consumers only extend specific parts of your hierarchy, sealed classes can offer that control.
  3. Exhaustive Pattern Matching: When using pattern matching and you want to ensure all possible cases are handled.

When Not to Use:

  1. Open-ended Scenarios: If you believe there’s a chance that other developers might need to extend your class in ways you haven’t thought of, it’s best not to seal it.
  2. For Simplicity: For simple projects or basic class hierarchies where exhaustive control isn’t beneficial, adding sealed class restrictions might be overkill.

Conclusion:

Sealed classes are a powerful addition to the Java language, offering a level of control over inheritance that wasn’t previously available. Like all features, they have their pros and cons, and the decision to use them should be based on the specific needs of the project and the problems you’re trying to solve.




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *