DEV Community

Cover image for Advanced Class Design using Java Sealed Classes
MyExamCloud
MyExamCloud

Posted on

Advanced Class Design using Java Sealed Classes

Introduction

In object-oriented programming, class design plays a crucial role in creating robust and maintainable code. With the release of Java 15, a new feature called sealed classes has been introduced, adding an extra layer of control and security to class design. Sealed classes allow developers to restrict class hierarchies, preventing external classes from extending or implementing them. In this article, we will discuss the concept of sealed classes, their benefits, and how they can be implemented in Java.

What are sealed classes?

A sealed class is a new type of class introduced in Java 15, that restricts the inheritance and implementation of its subclasses. It can be considered as a sealed container that allows only specific classes to extend or implement it. Once a class is declared as sealed, it becomes final by default, and any attempt to extend or implement it by external classes will result in a compilation error.

Advantages of using sealed classes

1. Enhanced Security: Sealed classes provide an additional layer of security by restricting the inheritance and implementation of its subclasses. This prevents the unauthorized modification of core classes and ensures that only trusted subclasses can access and modify the sealed class.

2. Better Control over Class Hierarchies: With sealed classes, developers have better control over the class hierarchies. By explicitly defining the allowed subclasses, we can ensure that only relevant and properly designed classes extend or implement the sealed class, leading to a more maintainable codebase.

3. Improved Code Flexibility: By restricting the subclasses that can extend or implement a sealed class, we can make necessary changes or refactoring without worrying about breaking any external code. This provides a sense of flexibility while working with sealed classes and ensures that the codebase remains stable and maintainable.

Implementation of Sealed Classes

To declare a class as sealed, we can use the 'sealed' keyword in the class declaration as shown below:

//Sealed class declaration
public sealed class Triangle permits EquilateralTriangle, RightAngleTriangle {

    //Class body
}
Enter fullscreen mode Exit fullscreen mode

Here, the 'permits' keyword is used to specify the subclasses that are allowed to extend the sealed class. In the above example, only the classes 'EquilateralTriangle' and 'RightAngleTriangle' can extend the 'Triangle' class.

Now, let's create the subclasses that can extend the 'Triangle' class.

//Example of Subclass that Extends Sealed Class
public final class EquilateralTriangle extends Triangle {
    //Class body
}

//Example of Subclass that Extends Sealed Class
public non-sealed class RightAngleTriangle extends Triangle {
    //Class body
}
Enter fullscreen mode Exit fullscreen mode

Notice that the 'EquilateralTriangle' class is declared as 'final' since it is the last subclass in the inheritance hierarchy, and the 'RightAngleTriangle' class is declared as 'non-sealed' since it allows further subclasses to extend it.

Next, let's see an example of implementing sealed interfaces. An interface can be declared as sealed using the same syntax as sealed classes.

//Sealed interface declaration
public sealed interface Shape permits Rectangle, Circle {

    //Interface body
    public double getArea();
}
Enter fullscreen mode Exit fullscreen mode

Here, the 'permits' keyword is used to specify the classes that are allowed to implement the sealed interface. In the above example, only the classes 'Rectangle' and 'Circle' can implement the 'Shape' interface.

//Example of Class Implementing Sealed Interface
public final class Rectangle implements Shape {

    //Interface method implementation
    public double getArea() {
        //Logic to calculate area of rectangle
    }
}

//Example of Class Implementing Sealed Interface
public non-sealed class Circle implements Shape {

    //Interface method implementation
    public double getArea() {
        //Logic to calculate area of circle
    }
}
Enter fullscreen mode Exit fullscreen mode

Notice that the 'Rectangle' class is declared as 'final' since it is the last class to implement the 'Shape' interface, and the 'Circle' class is declared as 'non-sealed' since it allows further classes to implement it.

Inheritance rules for sealed classes and interfaces

  1. A sealed class must explicitly permit the subclasses that can extend it.
  2. All the permitted subclasses must be direct subclasses of the sealed class. This means that we cannot extend a non-permitted subclass to create an indirect subclass of the sealed class.
  3. A sealed interface must explicitly permit the classes that can implement it.
  4. All the permitted classes must implement all the interface methods. This means that we cannot create a class that partially implements the sealed interface.

Conclusion

Sealed classes are a powerful addition to Java that can greatly enhance class design and make the codebase more secure and maintainable. By restricting the hierarchy of classes and interfaces, sealed classes provide an additional layer of control and flexibility while designing classes. With this new feature, developers can ensure that only trusted subclasses or implementing classes can access and modify the sealed class, leading to a more stable and robust codebase. Enhance your Java skills by taking latest Java Certifications.

Top comments (0)