What are sealed classes in Java 17

We Are Going To Discuss About What are sealed classes in Java 17. So lets Start this Java Article.

What are sealed classes in Java 17

  1. What are sealed classes in Java 17

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

  2. sealed classes in Java 17

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

Solution 1

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.

Advertisements

Original Author JDTheOne Of This Content

Solution 2

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.

Advertisements

Original Author Panagiotis Bougioukos Of This Content

Solution 3

Sealed Classes

  • A sealed class is a class or interface which restricts which other classes or interfaces may extend it.
  • Sealed classes and interfaces represent restricted class hierarchies that provide more control over an inheritance.
  • All direct subclasses of a sealed class are known at compile time. No other subclasses may appear after a module with the sealed class is compiled.
  • For example, third-party clients can’t extend your sealed class in their code. Thus, each instance of a sealed class has a type from a
    limited set that is known when this class is compiled.

Defining Sealed Classes

To seal a class, add the sealed modifier to its declaration. Then, after any extends and implements clauses, add the permits clause. This clause specifies the classes that may extend the sealed class.

For example, the following declaration of Shape specifies three permitted subclasses, Circle, Square, and Rectangle:

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

Define the following three permitted subclasses, Circle, Square, and Rectangle, in the same module or in the same package as the sealed class:

public final class Circle extends Shape {
    public float radius;
}

public non-sealed class Square extends Shape {
   public double side;
}   

public sealed class Rectangle extends Shape permits FilledRectangle {
    public double length, width;
}

Rectangle has a further subclass, FilledRectangle:

public final class FilledRectangle extends Rectangle {
    public int red, green, blue;
}

Constraints on Permitted Subclasses

  • They must be accessible by the sealed class at compile time.

    For example, to compile Shape.java, the compiler must be able to
    access all of the permitted classes of Shape: Circle.java,
    Square.java, and Rectangle.java. 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:

    1. final: Cannot be extended further
    2. sealed: Can only be extended by its permitted subclasses
    3. 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
    The Square is non-sealed.

  • They must be in the same module as the sealed class (if the sealed
    class is in a named module) or in the same package (if the sealed
    class is in the unnamed module, as in the Shape.java example).

For example, in the following declaration of com.example.graphics.Shape, its permitted subclasses are all in different packages. This example will compile only if Shape and all of its permitted subclasses are in the same-named module.

package com.example.graphics;

    public sealed class Shape 
        permits com.example.polar.Circle,
                com.example.quad.Rectangle,
                com.example.quad.simple.Square { }
Advertisements

Original Author Saurabh Dhage Of This Content

Solution 4

As per this documentation, Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. It is more of a declarative way to restrict the use of a superclass rather than using access modifiers.

In Java, a class can be final so no other classes can subclass it. If a class is not final, then it is open to all other classes to support code reusability. Doing so would raise data modeling concerns.

The below NumberSystem class is open to all classes, so any subclass can extend it. What if you want to restrict this NumberSystem to a fixed set of subclasses (Binary, Decimal, Octal, and HexaDecimal)?. It means you don’t want any other arbitrary class to extend this NumberSystem class.

class NumberSystem { ... }
final class Binary extends NumberSystem { ... }
final class Decimal extends NumberSystem { ... }
final class Octal extends NumberSystem { ... }
final class HexaDecimal extends NumberSystem { ... }

Using sealed class, you can achieve it by controlling the subclasses that can extend it and prevent any other arbitrary class from doing so.

Advertisements

Original Author Praj Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment