Home Java Java Sealed Classes: The Ultimate Guide to Secure Code

Java Sealed Classes: The Ultimate Guide to Secure Code

Published: Last Updated on 2 comments

Hey Tea Lovers! We meet after a long time. Today’s post will be a short and cut-to-the-point post about the latest offering (relatively) by Java 17+. Yes, as you read in the title, it is about sealed classes in java. We will go over some of the basics and will give you some pointers that I think fully describes it. So let’s get to it.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Introduction of Java’s Sealed Class

The sealed class in Java is a new addition to the core library of java since Java 17. It is an introduction to the new keywords, sealed and non-sealed. The sealed class allows one to describe which class/interface can extend/implement the current class/interface.  I will be using class throughout the post but it applies to both classes and interfaces in Java.

As the name suggests, it seals the class, and only authorized personnel (classes) can access it. But is that all? No, there are certain conditions to it. Let’s see them in detail.

⚠️ You will be needing Java 17+ JDK to run this

The sealed keyword in Java

To make a class sealed, you need to use the sealed the keyword before the class. The class can be a normal class, an interface, or an abstract class. One condition of the sealed classes is that those need to be extended or implemented by at least one class. There are no further changes inside the class or interface, this is just an external change. The following is the snippet that gives you a better picture.

sealed interface A { ... }
sealed class A { ... }
sealed abstract class A { ... }Code language: PHP (php)

Now, how does it know which subclasses are allowed and which are not? To know about it is uses an optional permits keyword. Let us see how.

The permits keyword in Java

The permits keyword in Java, helps the sealed class determine which subclasses are allowed to extend it. We have to put in the allowed classes or interfaces after permits the keyword, separated by a comma. Any other classes trying to extend them would get a compilation error saying you can’t extend the sealed class and it is not in the permit hierarchy.

sealed class A permits B, C, D { ... }

sealed interface permits B, C, D { ... }Code language: Java (java)

All looks good now. We now are secure from unwanted extends and can live happily ever after. No, there are some more things to do. Or as Steve Jobs said, “One more thing”.

How to extend or implement a sealed class in Java

When you extend this sealed class for the allowed subclasses, you will get an error. The error says that you need to add non-sealed, final or sealed keyword to the subclass.

We need to add the mentioned keyword before the class keyword and we are good to go. Important to notice, when using sealed keyword for the subclass, we again have to satisfy the conditions we mentioned earlier. Such as having at least one subclass.

final class B implements A  { ... }
non-sealed class C implements A  { ... }

final class D extends A  { ... }
sealed class B extends A  { ... } // need atleast one sublcass o f B or error wil be throw
Code language: Java (java)

Now, when we try to extend the class A in a non-permitted class, we will get the compilation error.

final class F extends A {...}
// Compilation Error:  'F' is not allowed in the sealed hierarchy of class ACode language: PHP (php)

The non-sealed Keyword in Java

The sealed class’s topic wouldn’t be complete without discussing the non-sealed keyword. Earlier I said, the subclass of the sealed class must be tagged with sealed, final or non-sealed keywords. We know that once we make the class final we can not extend it. As for the sealed subclass, we again must have a subclass for it.

The non-sealed keyword is unique. It kind of lets you bypass the sealed property or conditions via its own subclass. Meaning, it explicitly tells us that the sealed property wouldn’t be applied to this class. So we must be careful when using this. Or this would be a bypass to the sealed class.

It’s quite confusing, let us see this with some examples. Suppose, we have sealed class A, and only permit a non-sealed subclass B. Because B is not a final class, we can extend it. Let us extend it to class C. Now due o transitive dependency theory, C also extends A. A -> B -> C then A -> C.

sealed  class A permits B { ... }

non-sealed class B extends A { ... }

class C extends B { ... }
Code language: Java (java)

Some High-Level Rules for sealed Class

Here are some high-level pointers to keep in mind when using sealed classes. For the exhaustive detail about this, you can read the official JEP-409.

  1. Sealed classes must have at least one subclass
  2. Subclasses must be marked with sealed, non-sealed or final modifiers
  3. Only subclasses described after permits can extend it
  4. If permits is not given then any class can extend it, but needs to satisfy the 2nd condition
  5. You can still extend (transitively) the sealed class without a permit, by adding non-sealed keyword the subclass, and transitively inherit it.

Conclusion

That’s it for this post. We talked about the sealed keyword, and how to use it alongside the permit keyword in the latest Java. We also talked about the conditions that need to be satisfied in order to utilize the sealed classes offered by java versions 17+. Do let me know how you like this post and share your thoughts in the comment.

See you in the next post.

HAKUNA MATATA!!!!

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More