Skip to main content

What is Sealed Classes in Java 17

Java 17, released in September 2021, introduced several new features and enhancements. While some of the most prominent changes in Java 17 have received widespread attention, here's a lesser-known unique feature:

1. Sealed Classes: Sealed classes are a feature that allows developers to control the extent to which other classes can inherit from them. By declaring a class as sealed, you can specify which other classes are allowed to subclass it. This feature enhances encapsulation and provides more control over class hierarchies and extensions.

Sealed classes are declared using the `sealed` keyword, and the permitted subclasses are specified using the `permits` keyword. By default, a sealed class allows subclasses only from within the same module. However, you can also explicitly specify other modules that are permitted to subclass the sealed class.

This feature enables developers to create more secure and maintainable code by restricting inheritance to a defined set of classes, preventing uncontrolled extension of sealed classes.

It's worth noting that sealed classes are not widely known or commonly used, but they offer valuable benefits in terms of code organization and encapsulation.

Here's an example of how a sealed class can be defined in Java 17:


public sealed class Vehicle permits Car, Motorcycle {
    // Class implementation
}

final class Car extends Vehicle {
    // Car-specific implementation
}

final class Motorcycle extends Vehicle {
    // Motorcycle-specific implementation
}

class Bicycle extends Vehicle {
    // This will result in a compilation error because Bicycle is not permitted to subclass Vehicle.
}
```

In the example above, `Vehicle` is declared as a sealed class using the `sealed` keyword. The `permits` keyword specifies that only classes `Car` and `Motorcycle` are allowed to subclass `Vehicle`.

The `Car` and `Motorcycle` classes, which extend `Vehicle`, are explicitly declared as `final` classes. This indicates that they are the only permitted subclasses.

On the other hand, the `Bicycle` class attempts to subclass `Vehicle`, but since it is not listed in the `permits` clause, it results in a compilation error.

By using sealed classes, you can control the hierarchy of subclasses and prevent unexpected extensions from classes that are not explicitly permitted. This improves code maintainability and reduces the likelihood of unintended class inheritance.

Note: Sealed classes can have subclasses within the same module by default, but you can also explicitly permit subclasses from other modules if needed.

Comments

Popular posts from this blog

The relationship between Spring Data repositories methods and `Optional`

 In Spring Data repositories, the relationship between repository methods and `Optional` can vary depending on the specific use case and method signature. 1 . `Optional` as a return type:    When a repository method is defined to return a single entity or an optional result, you can use the `Optional` type as the return type. This indicates that the method may or may not find a matching entity in the database.    Example:    ```java    Optional<YourEntity> findById(Long id);    ```    In this case, the `findById` method is defined to return an `Optional<YourEntity>`. If the entity exists in the database, the `Optional` will contain the entity. Otherwise, it will be empty. 2. `Optional` as a parameter type:    You can also use `Optional` as a parameter type to indicate an optional input value for a repository method. This allows you to handle scenarios where a parameter may or may not be present.    Example:    ```java    List<YourEntity> findByCategory(Optional<Str

REST API for uploading file in chunks and merging them on the server-side.

Design Summary for Spring Boot REST API with Chunked File Upload: This design outlines a Spring Boot REST API for receiving file chunks and merging them on the server-side. The API allows clients to upload large files in smaller chunks, improving upload reliability and efficiency. Introduction: The system provides a RESTful API for uploading and merging file chunks. It addresses the challenge of uploading large files over HTTP efficiently. System Architecture: Utilizes a single Spring Boot application. RESTful architecture for client-server interaction. Relies on Spring's built-in components for handling HTTP requests. Data Design: Files are stored temporarily in memory. Utilizes an in-memory map to store uploaded chunks temporarily. Chunks are merged upon receiving all required parts. User Interface Design: This below design outlines a Spring Boot REST API that uses Apache Commons FileUpload to efficiently handle file uploads. It demonstrates a more advanced approach for handling