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
🌐
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 ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Sealed Classes
October 20, 2025 - They must have exactly one of the ... 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....
🌐
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.
🌐
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.
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.

🌐
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.
🌐
Rollbar
rollbar.com › home › what are sealed classes in java?
Beginner’s Guide to Sealed Classes in Java | Rollbar
November 10, 2023 - Sealed classes are a feature introduced in Java 15 which bring a new level of control and predictability to class hierarchies. A sealed class is a class that explicitly specifies which other classes are allowed to extend it.
Find elsewhere
🌐
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, AuthenticationMethod is a sealed class that permits PasswordMethod, TokenMethod, and BiometricMethod to extend it. By using sealed classes to define a limited set of authentication methods, developers can ensure that each method ...
🌐
HappyCoders.eu
happycoders.eu › java › sealed-classes
Sealed Classes in Java
June 12, 2025 - public class Shape { ... } public class Circle extends Shape { ... } public class Rectangle extends Shape { ... } public class Square extends Shape { ... } public class WeirdShape extends Shape { ... } public class TranspRectangle extends Rectangle { ... } public class FilledRectangle extends Rectangle { ... }Code language: Java (java) Usually, every developer can extend this class hierarchy at any place. An extended structure could look like this (I've colored the added classes light yellow): Sealed classes example - extension possibilities without sealing
🌐
Medium
medium.com › @kumar_sachin › understanding-sealed-classes-in-java-17-b542550b7d5e
Understanding Sealed Classes in Java 17 | by Sachin Kumar | Medium
February 16, 2024 - For example, in a geometry application, you might have a Shape class as sealed, and Circle, Square, and Triangle classes as its subclasses. This ensures that only these specific shapes can be created, preventing the introduction of unexpected ...
🌐
xperti
xperti.io › home › java feature spotlight: sealed classes
Sealed classes: What makes this Java feature so exciting?
September 5, 2022 - The example mentioned earlier in the article makes a statement about how a ‘Vehicle’ can only either be a: ... It means that the set of all Vehicles is equal to the set of all Cars, all Boats and all Planes combined. This is why sealed classes are also known as “sum types.” Because their value set is the sum of the value sets of a fixed list of other types. Sum types, and sealed classes are new for Java but not in the larger scale of things.Scala and many other high-level programming languages have been using sealed classes too, as well as sum types for quite some time.
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Java sealed classes: permits, non-sealed and pattern matching
1 month ago - A sealed hierarchy expresses the intended model directly in the Java type system. public sealed interface Transaction permits StockTransaction, BondTransaction, CryptoTransaction { } Each permitted implementation must then explicitly state what happens to inheritance below it. public final class StockTransaction implements Transaction { } public final class BondTransaction implements Transaction { } public final class CryptoTransaction implements Transaction { }
🌐
Medium
medium.com › @toimrank › java-sealed-classes-ef82d1a5ab2f
Java Sealed Classes. Sealed classes is a powerful feature… | by Imran Khan | Medium
January 17, 2025 - Exhaustive Pattern Matching: Sealed classes work well with pattern matching, allowing the compiler to perform exhaustive checks and ensure all possible cases are handled. Sample Code Let’s look at an example of using sealed classes to model a hierarchy of shapes:
🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
An interface can be marked as sealed ... Employee, Manager { } In this example, we've created a sealed interface Person which permits Employee and Manager interfaces to extend it....
🌐
AlgoMaster
algomaster.io › home › java › sealed classes
Sealed Classes | Java | AlgoMaster.io
June 2, 2026 - If a subtype lived in a faraway package or a separate module, the sealed parent couldn't reliably know about it, and the closed-list guarantee would weaken. For most code, the practical version is: keep the parent and the permitted subtypes in the same .java source file or in sibling files in the same package directory. ... Move PhysicalOrder into the com.store.orders package so it sits next to Order. The compiler error reads class is not allowed to extend sealed class: different package.
🌐
Medium
medium.com › @fullstacktips › sealed-classes-in-java-17-a22e4b0569e4
Java 17's Latest Feature: Sealed Classes for Improved Performance and Security | Medium
February 26, 2023 - In other words, they provide more control over how a class can be extended or implemented. To understand this better, let’s take an example of a Shape class, which can be extended by Circle, Square, or Triangle classes.
🌐
freeCodeCamp
freecodecamp.org › news › write-better-java-code-pattern-matching-sealed-classes
How to Write Better Java Code using Pattern Matching and Sealed Classes
January 20, 2026 - In this example, we've defined a sealed class called Result which can be extended by either Success or Failure classes. Any other class that attempts to extend Result will result in a compilation error.