DEV Community

Cover image for Can Sealed Interfaces in Java Improve Code Security?
Bhagyashri Birajdar
Bhagyashri Birajdar

Posted on

Can Sealed Interfaces in Java Improve Code Security?

In the field of software development, security and robustness are of utmost importance to protect against potential vulnerabilities.

Sealed interfaces in Java17 are a simple & powerful way to enhance code security.

In our previous blog post, We covered the concept of Sealed Classes. Now, it's time to turn our attention towards sealed interfaces & how they improve code security. But before that, you must have an idea about what an interface is.

In Java 17, sealed classes and sealed interfaces share similar rules and behavior.

Like sealed classes, to seal an interface, add the sealed keyword to it's declaration. After "extends" statement, include a permits clause (If there are no "extends" statements, you can skip that part). permits clause tells which classes are allowed to implement this sealed interface and which interfaces can extend it.

When an interface is marked as sealed, any class or interface attempting to implement or extend it must be listed in the permits clause of the sealed interface. Otherwise, it will result in a compilation error.

This way, you control who can work with your interface, ensuring everything stays well-organized and secure.

syntax

public sealed interface Interface_Name permits Subinterface1, Subinterface2, ... {
    // Interface members and methods
}
Enter fullscreen mode Exit fullscreen mode

example

sealed interface Color permits Red, Green {
    default void display() {
        System.out.print("This is Color interface");
    }
}
Enter fullscreen mode Exit fullscreen mode

This Java code defines a sealed interface named "Color" that permits implementation by the interface "Red" and the class "Green". It includes a default "display()" method which prints "This is Color interface" when called.

non-sealed interface Red extends Color {
    default void show() {
        System.out.println("This is Red interface");
    }
}
Enter fullscreen mode Exit fullscreen mode

This code defines a non-sealed interface named "Red" which extends the "Color" interface. It includes a default method "show()" that prints "This is Red interface" when called.

final class Green implements Color {
    public void play() {
        System.out.println("This is Green class");

    }
}
Enter fullscreen mode Exit fullscreen mode

The code defines a final class named "Green" which implements the interface "Color". It includes a method "play()" that prints "This is Green class". The use of final means that "Green" class cannot be subclassed or extended further.

Code Execution

public class Main {
    public static void main(String[] args) {
        Color obj2 = new Green();
        obj2.display();

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

This is Color interface
Enter fullscreen mode Exit fullscreen mode

Records can be used with Sealed Interfaces. In our next blog post, we'll explore what a record is and how records and sealed interfaces work together. Gear up for an exciting exploration of these Java features! Stay tuned!

Top comments (1)

Collapse
 
niihal profile image
Nihal Patel

nice..