You can follow this link for examples.

In short, sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.

Example from the link:

public sealed interface Service permits Car, Truck {

    int getMaxServiceIntervalInMonths();

    default int getMaxDistanceBetweenServicesInKilometers() {
        return 100000;
    }
}

This interface only permits Car and Truck to implement it.

Answer from JDTheOne on Stack Overflow
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Sealed Classes
October 20, 2025 - In addition, because Rectangle is a sealed class, the compiler also needs access to FilledRectangle.java. They must directly extend the sealed class. They must have exactly one of the following modifiers to describe how it continues the sealing initiated by its superclass: ... non-sealed: Can be extended by unknown subclasses; a sealed class cannot prevent its permitted subclasses from doing this · For example, the permitted subclasses of Shape demonstrate each of these three modifiers: Circle is final while Rectangle is sealed and Square is non-sealed.
Discussions

class - What are sealed classes in Java 17? - Stack Overflow
Note: Any attempt to create a subclass ... error java: invalid permits clause. ... Save this answer. ... Show activity on this post. A sealed class lets you control the class hierarchy more precisely by explicitly specifying the permitted subclasses. This is useful for maintaining control over inheritance, improving security, and enabling better compiler checks and pattern matching. Here's a practical example using a sealed ... More on stackoverflow.com
🌐 stackoverflow.com
Sealed class in Java
They're very useful for modelling sum types (aka disjoint union types). Using sealed types then allows exhaustiveness checks when using pattern matching over the type. You can achieve the same without sealed types but it's much more verbose and brittle. More on reddit.com
🌐 r/java
27
29
September 6, 2023
Java Sealed Classes
I'm looking forward to this! I love it when the type system / compiler can keep me from making mistakes. More on reddit.com
🌐 r/java
44
87
June 17, 2020
Does anyone else not like the non-sealed keyword?
There's this maxim: "Don't assume your programmers are idiots". But that only goes so far. Let me put it this way: If you release a language feature, and 90% of the code out there is clearly being idiotic about using it, part of the blame surely falls on the language designer. I don't think the argument: "But everybody is using it wrong" absolves you of all blame in such a scenario. What you desire here, which is that public class Foo implements SomeSealedThing, sure seems to me like it would do precisely that: Go wrong 90%+ of the time. Accidentally unsealing a hierarchy (which is what you're doing here, if it worked the way you wanted it) is not the kind of bug a unit test is going to catch either, for what that's worth. Surely most here would agree this is going to end up being misused 90%+ of the time if the compiler doesn't help you out, right? I can see a system whereby any non-'permits'-claused thing is automatically and silently made final, except what if later on the sealed thing you are inheriting from loses the 'sealed' property? Now, silently, your class's finality changes. I'm not saying non-sealed is fantastic. I'm merely saying: It's better than every obvious alternative I've seen so far. More on reddit.com
🌐 r/java
43
31
September 6, 2020
🌐
JavaTechOnline
javatechonline.com › home › sealed class in java
Sealed Class In Java With Examples
May 3, 2026 - In this example, Shape is declared as a sealed abstract class with permits Circle, Triangle, meaning only these two classes can extend Shape. Circle and Triangle are both final classes, indicating that they cannot be further subclassed.
🌐
Baeldung
baeldung.com › home › java › core java › sealed classes and interfaces in java
Sealed Classes and Interfaces in Java | Baeldung
December 11, 2025 - When creating the Vehicle abstract class in Java, we should be able to allow only Car and Truck classes to extend it. As such, we want to ensure that there will be no misuse of the Vehicle abstract class within our domain. In this example, we’re more interested in the clarity of code handling known subclasses, than defending against all unknown subclasses. Before version 15 (in which sealed ...
🌐
Medium
medium.com › @ByteCodeBlogger › sealed-classes-examples-of-real-life-use-case-java-library-use-case-3a36d79e5bce
Sealed Classes : Examples of Real-life use case & Java Library use case | by Full Stack Developer | Medium
September 1, 2024 - This is done using the permits clause. public sealed class Vehicle permits Car, Truck { } 2 . Permitted Subclasses: Subclasses of a sealed class must be one of the following: Final: No further subclasses are allowed.
🌐
GeeksforGeeks
geeksforgeeks.org › java › sealed-class-in-java
Sealed Class in Java - GeeksforGeeks
2 weeks ago - A sealed class in Java is a class that restricts which other classes can extend it. The permitted subclasses are explicitly specified using the permits clause. This provides more control over inheritance than a normal class.
🌐
Rollbar
rollbar.com › home › what are sealed classes in java?
Beginner’s Guide to Sealed Classes in Java | Rollbar
November 10, 2023 - The sealed modifier is used to declare a class as sealed. Additionally, the classes that are permitted to be its direct subclasses are specified using the permits keyword. Here’s an example:
Find elsewhere
Top answer
1 of 10
67

You can follow this link for examples.

In short, sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.

Example from the link:

public sealed interface Service permits Car, Truck {

    int getMaxServiceIntervalInMonths();

    default int getMaxDistanceBetweenServicesInKilometers() {
        return 100000;
    }
}

This interface only permits Car and Truck to implement it.

2 of 10
38

The JEP 409 explains it as

A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.

A more practical explanation is the following:

The situation in the past was:

  • You could not restrict an interface being extended by another interface
  • You could not constraint which classes where able to implement a specific interface.
  • You had to declare a class as final in order to not be extended by another class. This way no class could extend the declared final class. This was black or white approach.

The current situation with sealed keyword is:

  • You can now restrict an interface being extended by other interfaces and make a rule for only some specific interfaces which will be allowed to extend it.

    Example:

    public sealed interface MotherInterface permits ChildInterfacePermitted {}
    
    //Has to be declared either as sealed or non-sealed
    public non-sealed interface ChildInterfacePermitted extends MotherInterface {}  
    
    public interface AnotherChildInterface extends MotherInterface {} 
    //compiler error! It is not included in the permits of mother inteface
    
  • You can now create an interface and select only specific classes that are allowed to implement that interface. All other classes are not allowed to implement it.

    Example:

     public sealed interface MotherInterface permits ImplementationClass1 {} 
    
     //Has to be declared either as final or as sealed or as non-sealed
     public final class ImplementationClass1 implements MotherInterface {} 
    
     public class ImplementationClass2 implements MotherInterface {} 
     //compiler error! It is not included in the permits of mother inteface
    
  • You can now restrict a class being extended (same as before with final) but you can now allow some specific classes to extend it. So now you have more control as before the keyword final was absolute restricting every class from extending the declared final class

    Example:

    public sealed class MotherClass permits ChildClass1 {}
    
    //Has to be declared either as final or as sealed or as non-sealed
    public non-sealed class ChildClass1 extends MotherClass {} 
    
     public class ChildClass2 extends MotherClass {} 
     //compiler error! It is not included in the permits of MotherClass
    

Important notes:

  • The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, to the same package.

    Example:

    Let's say that we have the same unnamed module and the following packages

      -packageA
         -Implementationclass1.java
      -packageB
         -MotherClass.java
    

    or

       -root
          -MotherClass.java
          -packageA
             -Implementationclass1.java
    

    You will get the error Class is not allowed to extend sealed class from another package. So if you have an unnamed module all participating classes and interfaces for the sealed function must be placed exactly on the same package.

  • Every permitted subclass must directly extend the sealed class.

🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
package com.tutorialspoint; public class Tester { public static void main(String[] args) { // create an instance of Manager Person manager = new CorpManager(23, "Robert"); // get the id System.out.println("Id: " + getId(manager)); } public static int getId(Person person) { // check if person is employee then return employee id if (person instanceof Employee) { return ((Employee) person).getEmployeeId(); } // if person is manager then return manager id else if (person instanceof Manager) { return ((Manager) person).getManagerId(); } return -1; } } // a sealed interface Person which is to be inh
🌐
Foojay
foojay.io › home › java sealed classes in action: building robust and secure applications
Java Sealed Classes: Building Robust and Secure Applications
March 2, 2023 - In this example, Shape is a sealed interface that only permits Circle and Square to implement it. This ensures that any other implementations of Shape cannot be created. Sealed classes can also be used to enhance pattern matching in switch ...
🌐
Medium
medium.com › @toimrank › java-sealed-classes-ef82d1a5ab2f
Java Sealed Classes. Sealed classes is a powerful feature… | by Imran Khan | Medium
January 17, 2025 - In this example, Shape is a sealed class that permits Circle, Rectangle, and Square to extend it. Circle and Square are final classes, while Rectangle is a sealed class that permits Square to extend it.
🌐
Medium
medium.com › codex › sealed-classes-in-java-17-8f00351d27f4
Sealed Classes in Java 17. An Introduction | by Afroz Chakure | CodeX | Medium
August 12, 2024 - ... Above, we have defined a sealed ... Bike { void start(); } In this example, we have defined a sealed interface calledVehiclewhich permits only Car and Bike classes to implement it....
🌐
HappyCoders.eu
happycoders.eu › java › sealed-classes
Sealed Classes in Java
June 12, 2025 - So the following is valid Java code: public void sealed() { int permits = 5; }Code language: Java (java) In Java 17, "Pattern Matching for switch" was introduced as a preview feature.
🌐
Hungrycoders
hungrycoders.com › blog › sealed-classes-and-records-in-java
Sealed Classes and Records in Java
In below combined example, Shape is a sealed interface, and Circle, Rectangle, and Square are records that implement this interface. This approach offers both conciseness and controlled inheritance.
🌐
DEV Community
dev.to › adaumircosta › mastering-sealed-classes-in-java-3md
Mastering Sealed Classes in Java - DEV Community
July 25, 2024 - In this example, Shape is a sealed class, and only Circle, Rectangle, and Square are permitted to extend it. Each subclass must be final, sealed, or non-sealed. Sealed classes can be used to model hierarchical structures where the set of subclasses ...
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Use sealed classes in Java to control your inheritance
4 weeks ago - Learn how Java sealed classes control inheritance with sealed, permits, final and non-sealed, and how closed hierarchies enable exhaustive pattern matching with switch.