Why Sealed Classes in Java Are Gamechangers for OOP?

This article provides brief information on Sealed Classes in Java by comparing it with the Abstract class and Interface.

Eidan Khan
Make Android
7 min readDec 27, 2023

--

In the world of object-oriented programming, Java continues to evolve with new features that enhance code organization, readability, and maintainability. One such feature introduced in recent versions of Java 17 is sealed classes. Sealed classes provide a powerful mechanism to control and define the hierarchy of subclasses within a class, bringing a new level of control and precision to class design.

Understanding Sealed Classes

At its core, a sealed class is a class that restricts the hierarchy of its subclasses. In other words, it explicitly specifies which classes are allowed to extend it. This introduces a level of predictability and control over the inheritance structure, preventing unauthorized or unintended subclasses from being created.

Sealed classes are particularly valuable in scenarios where you want to ensure that certain classes have exclusive access to extending a base class. By carefully defining the permitted subclasses, you can avoid the potential pitfalls of a wide and unregulated class hierarchy.

Benefits of Using Sealed Classes

The adoption of sealed classes in Java brings a multitude of benefits that contribute to better code organization and maintainability:

Enhanced Code Predictability: Sealed classes eliminate the uncertainty that can arise from an open-ended inheritance model. By explicitly specifying which subclasses are allowed, you can accurately predict and manage the possible extensions of a class. This predictability enhances the overall stability and reliability of your codebase.

Improved Maintainability: In large codebases, managing an extensive class hierarchy can become challenging. Sealed classes streamline this process by clearly defining the relationships between classes. This makes it easier to understand the architecture of the code and facilitates more efficient maintenance and updates.

Precise Access Control: Sealed classes offer a fine-grained approach to access control. You can control exactly which classes are permitted to extend a sealed class, preventing unauthorized extensions. This level of control helps in enforcing design principles and architectural decisions.

Reducing Bugs and Errors: Unintended and unauthorized subclassing can lead to unexpected behaviour and bugs. Sealed classes mitigate this risk by constraining the hierarchy. This reduction in potential errors contributes to a more robust and bug-free codebase.

In the upcoming sections of this article, we will dive deeper into the mechanics of sealed classes, explore how to declare and extend them and showcase real-world scenarios where sealed classes shine.

Declaring a Sealed Class

To declare a sealed class, you use the sealed modifier before the class keyword. The sealed keyword restricts the inheritance of the class to a specific set of subclasses. These subclasses are explicitly declared using the permits clause.

Here’s a basic syntax for declaring a sealed class:

public sealed class Shape permits Circle, Square, Triangle {
// Class members and methods
}

In this example, the Shape class is declared as sealed, and it permits the subclasses Circle, Square, and Triangle to extend it. This means that no other class can extend Shape unless it is explicitly specified in the permits clause.

Extending a Sealed Class

Subclasses of a sealed class must explicitly declare their inheritance using the extends keyword and the name of the sealed class they intend to extend. Additionally, the subclass must be one of the permitted subclasses declared in the sealed class.

Here’s an example of extending a sealed class:

public final class Circle extends Shape {
// Class members and methods
}

In this case, the Circle class extends the sealed class Shape, and since Circle is one of the permitted subclasses of Shape, this extension is valid.

Benefits of Sealed Classes in Declaration and Extension

Declaring and extending sealed classes offers several advantages:

1. Clear Hierarchy
The permits clause provides a clear and concise way to define the allowed subclasses. This helps in maintaining a well-organized class hierarchy.

2. Enforced Design
Sealed classes enforce the design decision of allowing only specific subclasses. This ensures that the class hierarchy adheres to the intended architecture.

3. Readability and Documentation
Sealed classes make it easier to understand the relationships between classes. Developers can quickly identify which subclasses are permitted, leading to better code comprehension.

4. Prevention of Unintended Extensions
Sealed classes prevent unintentional extensions by restricting the possibilities of inheritance. This contributes to code stability and reduces the chances of introducing bugs.

Real-World Use Cases of Sealed Classes

Sealed classes in Java offer a versatile way to model complex scenarios and encapsulate behaviours within well-defined hierarchies. In this section, we will explore practical use cases of sealed classes and demonstrate how they can be employed to solve real-world problems.

1. Modeling State Machines with Sealed Classes
State machines are a fundamental concept in software engineering, often used to represent the behaviour of an object based on its current state and input. Sealed classes provide an elegant approach to model state machines by encapsulating different states and their corresponding behaviours as subclasses.

Let’s consider a simplified example of an order processing system using sealed classes:

sealed interface OrderState permits NewOrder, ShippedOrder, DeliveredOrder {
void processOrder();
}
final class NewOrder implements OrderState {
public void processOrder() {
System.out.println("Processing new order...");
}
}
final class ShippedOrder implements OrderState {
public void processOrder() {
System.out.println("Order has been shipped.");
}
}
final class DeliveredOrder implements OrderState {
public void processOrder() {
System.out.println("Order has been delivered.");
}
}

In this example, the OrderState interface represents the states of an order. Each state class, such as NewOrder, ShippedOrder, and DeliveredOrder, implements the OrderState interface and defines its specific behaviour. By using sealed classes, we ensure that the set of order states is well-defined and restrict extensions to authorized subclasses.

2. Implementing Domain-Specific Behaviors
Sealed classes are particularly useful for implementing domain-specific behaviours within a bounded context. By encapsulating behaviours within sealed subclasses, you can model distinct aspects of the domain and ensure that only valid variations are allowed.

Imagine a content management system where different content types require specialized rendering logic:

sealed class Content permits TextContent, ImageContent {
abstract String render();
}
final class TextContent extends Content {
private final String text;
public TextContent(String text) {
this.text = text;
}
public String render() {
return "<p>" + text + "</p>";
}
}
final class ImageContent extends Content {
private final String imageUrl;
public ImageContent(String imageUrl) {
this.imageUrl = imageUrl;
}
public String render() {
return "<img src=\"" + imageUrl + "\" alt=\"Image\">";
}
}

3. Solving Business Logic Scenarios
Sealed classes excel in solving complex business logic scenarios where a limited set of outcomes or options exist. By explicitly defining subclasses and their behaviours, you can model intricate decision-making processes.

Let’s consider a banking application that handles different types of transactions:

sealed class TransactionResult permits SuccessfulTransaction, FailedTransaction {
abstract void processTransaction();
}
final class SuccessfulTransaction extends TransactionResult {
private final double amount;
public SuccessfulTransaction(double amount) {
this.amount = amount;
}
public void processTransaction() {
System.out.println("Transaction successful. Amount: " + amount);
}
}
final class FailedTransaction extends TransactionResult {
private final String errorMessage;
public FailedTransaction(String errorMessage) {
this.errorMessage = errorMessage;
}
public void processTransaction() {
System.out.println("Transaction failed. Reason: " + errorMessage);
}
}

Sealed Classes vs. Abstract Classes and Interfaces

Java provides several mechanisms for class inheritance, including abstract classes and interfaces. Sealed classes introduce a new way to manage class hierarchies that offers distinct advantages over these traditional approaches.

Comparison between Sealed Classes and Abstract Classes

Sealed Classes:

  • Sealed classes restrict the hierarchy of subclasses that can extend them using the permits keyword.
  • Subclasses must be explicitly listed using permits in the sealed class declaration.
  • This restriction promotes better control and encapsulation of the class hierarchy.
  • Sealed classes support exhaustive pattern matching, ensuring comprehensive handling of all possible subclasses.
  • Intended for scenarios where a fixed set of subclasses should be tightly controlled.

Abstract Classes:

  • Abstract classes provide a base class that may include some concrete methods and abstract methods.
  • Subclasses can freely extend the abstract class and override its methods.
  • Abstract classes allow a more open-ended hierarchy, potentially leading to a wider range of subclasses.
  • Abstract classes are suited for sharing code and implementing common behavior among related classes.

Comparison between Sealed Classes and Interfaces

Sealed Classes:

  • Sealed classes define a closed set of subclasses using permits, much like an interface with a predefined set of implementing classes.
  • Sealed classes can include abstract methods that must be implemented by each subclass.
  • Sealed classes provide a balance between interface-like contracts and abstract class inheritance.
  • They offer more control over the hierarchy compared to traditional interfaces.

Interfaces:

  • Interfaces define a contract that multiple classes can implement.
  • Classes implementing an interface can belong to different inheritance hierarchies.
  • Interfaces support multiple inheritance, allowing a class to implement multiple interfaces.
  • Interfaces are often used for defining common behaviour across unrelated classes.

When to Use Sealed Classes

Sealed classes are particularly useful in scenarios where:

  • You want a well-defined and controlled set of subclasses.
  • Exhaustive pattern matching is crucial for ensuring comprehensive handling of subclasses.
  • You want to strike a balance between the restrictions of sealed classes and the flexibility of abstract classes or interfaces.
  • Your design involves a small, predefined set of roles or states that subclasses can represent.

We hope you enjoyed exploring Sealed Classes in Java through our article. If you found these insights valuable, feel free to share your thoughts and ideas. Your feedback and engagement drive us to create more informative content.

Happy coding!

Make Android

--

--

Eidan Khan
Make Android

🚀 Full-stack Dev | Tech Content Creator 📝 For more in-depth articles, tutorials, and insights, visit my blog at JavaJams.org.