Java 23 sealed classes explained

Connect With Us
Sign up for our newsletter

Sign up to our Newsletter to get the latest news and offers.

  • August 05,2025

Java 23 sealed classes explained

Java 23 sealed classes restrict which classes can extend or implement them, enhancing type safety and maintainability by explicitly defining a fixed set of permitted subclasses. This feature helps create controlled, secure inheritance hierarchies for better API design and exhaustive checks.

Java 23 Sealed Classes Explained

1 ) Introduction to Sealed Classes  

Sealed classes and interfaces in Java restrict which other classes or interfaces may extend or implement them. This feature, introduced to provide controlled inheritance, ensures that a predefined and limited set of subclasses can extend a sealed class, enhancing code safety and maintainability.

2 ) Purpose and Use Cases  

  Code Reuse with Control: Inheritance allows reuse of code, but unlimited subclassing can cause issues in domain modeling.  

  Modeling Finite Domain Entities: For example, a graphics library defining geometric shapes can seal the `Shape` class to restrict extensions to known primitives only (like Circle, Square, Rectangle).  

  Closed API Design: Prevents clients from arbitrarily extending classes, ensuring the API behaves predictably.

3 ) Declaring Sealed Classes  

  Use the `sealed` modifier in the class declaration.  

  Follow it with the `permits` clause listing all allowed subclasses.  

  Example:

   java

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

   

  Permitted subclasses must be declared in the same module or package.

4 ) Types of Permitted Subclasses  

  Final Classes: Cannot be further extended.  

  Non sealed Classes: Allowed to be extended by other arbitrary classes (breaking the seal if needed).  

  Sealed Classes: Can continue having a restricted set of subclasses with their own permits clause. 

Example subclasses:

 java

public final class Circle extends Shape { … }

public non sealed class Square extends Shape { … }

public sealed class Rectangle extends Shape permits FilledRectangle { … }

 

5 ) Permitted Subclasses Constraints  

  Must be accessible at compile time to the sealed class.  

  Must be explicitly declared through `permits` or be within the same file (where permits can be omitted).  

  Subclasses must respect visibility and inheritance constraints to maintain strong encapsulation.

6 ) Sealed Classes vs Sealed Interfaces  

  Both restrict which classes or interfaces may extend or implement them.  

  All direct subclasses or implementers are known at compile time, which facilitates exhaustive checks in control flow statements (e.g., `when` expressions in modern Java variants).  

7 ) Constructors in Sealed Classes  

  Sealed classes are inherently abstract and cannot be instantiated on their own.  

  Constructors are used to initialize subclasses.  

  Constructor visibility can be `protected` (default) or `private` for tighter control.  

  Example:

   java

  sealed class Error(val message: String) {

      class NetworkError : Error("Network failure")

      class DatabaseError : Error("Database cannot be reached")

  }

     

  Enums may be used alongside sealed classes for enhanced state representation.

8 ) Benefits of Sealed Classes  

  Enhanced type safety: Covers all known subclasses exhaustively in switch or conditional expressions.  

  Better maintainability: Limiting subclassing prevents unintentional or harmful extensions.  

  Improved API robustness: Guarantees that external clients cannot extend classes beyond the defined scope.

9 ) Summary  

Java 23’s sealed classes are a powerful feature for managing inheritance hierarchies by explicitly specifying permitted subclasses. This allows developers to design safer, more predictable, and maintainable class structures suited for domain modeling, state representation, and closed APIs.

 

 

https://justacademy.in/news-detail/flutter-test-lab-new-features

 

https://justacademy.in/news-detail/android-security-best-practices-updates

 

https://justacademy.in/news-detail/how-react-native-is-powering-the-next-generation-of-desktop-apps

 

https://justacademy.in/news-detail/java-in-cloud-security:-best-practices

 

https://justacademy.in/news-detail/ios-developer-jobs-on-the-rise-in-india

 

Related Posts