Sealed class in java
12/04/2025
In Java, a sealed class is a feature introduced in Java 15 (as a preview) and officially in Java 17 (as a standard feature). This feature allows you to restrict and control the inheritance hierarchy by limiting which classes or interfaces can extend or implement a sealed class or interface.
Sealed classes are especially useful in domain modeling, such as when defining a fixed set of payment methods (e.g., credit card, PayPal, bank transfer) or representing states in a finite state machine. The feature follows specific rules: classes or interfaces extending or implementing the sealed class must be in the same module (or same package if not using modules). If any class or interface not explicitly permitted tries to extend or implement it, the compiler will generate a compile-time error.
Implementation
// Sealed Class
public sealed interface PaymentMethod
permits CreditCardPayment, PayPalPayment, BitcoinPayment {
void processPayment(double amount);
}
package com.example.paymentMethod;
// Final: cannot be extended further
public final class CreditCardPayment implements PaymentMethod {
public void processPayment(double amount) {
System.out.println("Processing credit card payment of $" + amount);
}
}
package com.example.paymentMethod;
// Final subclass of a sealed subclass
public final class BitcoinPayment implements PaymentMethod {
public void processPayment(double amount) {
System.out.println("Processing Bitcoin payment of $" + amount);
}
}
package com.example.paymentMethod;
package com.example.paymentMethod;
// Non-sealed: removes restriction, anyone can extend
public non-sealed class PayPalPayment implements PaymentMethod {
public void processPayment(double amount) {
System.out.println("Processing PayPal payment of $" + amount);
}
}
public class Main {
public static void main(String[] args) {
PaymentMethod p1 = new CreditCardPayment();
PaymentMethod p2 = new PayPalPayment();
PaymentMethod p3 = new BitcoinPayment();
p1.processPayment(100);
p2.processPayment(150);
p3.processPayment(200);
}
}
Summary
In this example, the PaymentMethod interface is declared as sealed, which means
that only the classes explicitly listed in its permits clause CreditCardPayment,
PayPalPayment
, and BitcoinPayment are allowed to implement it.
CreditCardPayment and BitcoinPayment are marked as final, so they cannot be subclassed
further.
PayPalPayment could optionally be declared non-sealed if you want other classes to extend
it.
Sealed types must follow specific rules : ⬇️
Permitted subclasses must be expLicitly Listed in the permits clause.
They must be in the same package or module as the sealed type.
Each permitted subclass must be declared as final, sealed, or
non-sealed.
Any class not Listed in permits clause will trigger a compiler error if it tries to
implement or extend the sealed type.