Java sealed Keyword

The sealed keyword in Java is used to restrict which classes or interfaces can extend or implement a given class or interface. This feature, introduced in Java 17, allows developers to define a more controlled and predictable class hierarchy. A sealed class or interface specifies a list of permitted subclasses or implementors, providing more fine-grained control over inheritance.

Table of Contents

  1. Introduction
  2. sealed Keyword Syntax
  3. Understanding sealed
  4. Examples
    • Sealed Class
    • Sealed Interface
    • Permitting Classes
  5. Real-World Use Case
  6. Conclusion

Introduction

In Java, the sealed keyword allows you to declare a class or interface as sealed, meaning only specified subclasses or implementors can extend or implement it. This helps in creating a more controlled inheritance hierarchy and can be useful for maintaining security and predictability in your application’s design.

sealed Keyword Syntax

The syntax for declaring a sealed class or interface is as follows:

public sealed class ClassName permits Subclass1, Subclass2 {
    // class body
}

public sealed interface InterfaceName permits Implementor1, Implementor2 {
    // interface body
}

Example:

public sealed class Shape permits Circle, Rectangle, Square {
    // class body
}

Understanding sealed

Key Points:

  • Controlled Inheritance: The sealed keyword restricts which classes can extend or implement a class or interface.
  • Permits Clause: The permits clause specifies the allowed subclasses or implementors.
  • Subclasses: The subclasses must either be final, sealed, or non-sealed.

Types of Subclasses:

  1. final: No further subclassing is allowed.
  2. sealed: Further subclassing is restricted to specific classes.
  3. non-sealed: The class can be subclassed without restrictions.

Examples

Sealed Class

A sealed class with permitted subclasses.

Example

// File: Shape.java
public sealed class Shape permits Circle, Rectangle, Square {
    // common properties and methods
}

// File: Circle.java
public final class Circle extends Shape {
    // properties and methods specific to Circle
}

// File: Rectangle.java
public sealed class Rectangle extends Shape permits Square {
    // properties and methods specific to Rectangle
}

// File: Square.java
public final class Square extends Rectangle {
    // properties and methods specific to Square
}

Sealed Interface

A sealed interface with permitted implementors.

Example

// File: Operation.java
public sealed interface Operation permits Addition, Subtraction {
    int apply(int a, int b);
}

// File: Addition.java
public final class Addition implements Operation {
    @Override
    public int apply(int a, int b) {
        return a + b;
    }
}

// File: Subtraction.java
public final class Subtraction implements Operation {
    @Override
    public int apply(int a, int b) {
        return a - b;
    }
}

Permitting Classes

A sealed class that permits only specified classes to extend it.

Example

// File: Animal.java
public sealed class Animal permits Dog, Cat {
    // common properties and methods
}

// File: Dog.java
public final class Dog extends Animal {
    // properties and methods specific to Dog
}

// File: Cat.java
public final class Cat extends Animal {
    // properties and methods specific to Cat
}

Real-World Use Case

Financial Transactions

In a financial application, you might want to control the types of transactions that can occur, such as deposits and withdrawals, ensuring no other transaction types are inadvertently created.

Example

// File: Transaction.java
public sealed class Transaction permits Deposit, Withdrawal {
    // common properties and methods
}

// File: Deposit.java
public final class Deposit extends Transaction {
    // properties and methods specific to Deposit
}

// File: Withdrawal.java
public final class Withdrawal extends Transaction {
    // properties and methods specific to Withdrawal
}

Conclusion

The sealed keyword in Java provides a powerful mechanism for controlling inheritance and implementation hierarchies. By specifying which classes or interfaces can extend or implement a sealed class or interface, you can ensure a more predictable and secure design. Understanding and using the sealed keyword effectively is crucial for developing robust and maintainable Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top