How to write a Sealed Class ?

How to write a Sealed Class ?

In the last two articles, we discussed the what and why parts - as what's a sealed class and second, why do we even need this concept? Today it's time to see how do we write sealed classes or interfaces.

Just like the way we write abstract classes, or final classes by using specific modifiers, in the same way, Java has given us a modifier named sealed to make a class/interface sealed.

Now coming to the classes which are allowed to extend a sealed class or implement in case of a sealed interface, Java provides us another clause permits

In the below example, Shape class can be extended by only two other classes - Square, Circle

public abstract sealed class Shape permits Square, Circle { ..}        

Is it always mandatory to write a permits clause followed by name of the permitted subclasses?

Answer is NO. Then, how would we specify which classes are permitted ?

abstract sealed class Root { ...

??final class A extends Root { ... }

??final class B extends Root { ... }

??final class C extends Root { ... }

}?        

Worthy to learn here are some constraints which a sealed class mandates its subclasses to follow:

  1. The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, to the same package.
  2. Every permitted subclass must directly extend the sealed class.
  3. Every permitted subclass must use a modifier to describe how it propagates the sealing initiated by its superclass:

  • It may be declared?final?to prevent further inheritance.
  • It may be declared?sealed?to allow its part of the hierarchy to be extended further than envisaged by its sealed superclass, but in a restricted fashion.
  • It may be declared?non-sealed?means there is no restriction on subclass. A subclass can be extended further by unknown subclasses.

Exactly one of the modifiers?final,?sealed, and?non-sealed?must be used by each permitted subclass. Ending the article by a code snippet to explain above use cases -

package com.example.geometry;

public abstract sealed class Shape
    permits Circle, Rectangle, Square, WeirdShape { ... }


public final class Circle extends Shape { ... } 



public sealed class Rectangle extends Shape 
    permits TransparentRectangle, FilledRectangle { ... }

public final class TransparentRectangle extends Rectangle { ... }

public final class FilledRectangle extends Rectangle { ... }



public non-sealed class WeirdShape extends Shape { ... }        

It's not done yet. In next article, we'll learn how to write Sealed Interfaces.

Till then, Happy Learning!

要查看或添加评论,请登录

Shiv Dhiman的更多文章

  • Team Fridays | Decision-making activity around Conflicting views

    Team Fridays | Decision-making activity around Conflicting views

    This Friday's session with the team was around a decision-making activity around conflicting views on a problem…

  • Team Fridays | Learning & Sharing

    Team Fridays | Learning & Sharing

    Learning and sharing are key to the team's cohesiveness and growth. And, Fridays, I believe, are the best days to do so.

  • How to write a Sealed Interface ?

    How to write a Sealed Interface ?

    Finally, we've come to the last part of the Sealed concept. We've already learnt - what, why and how parts in last…

  • Why do we need a Sealed Class?

    Why do we need a Sealed Class?

    Hello Folks, In the last article here, we discussed about what is exactly the concept of Sealed? Today, we are going to…

  • What is a Sealed Class/Interface ?

    What is a Sealed Class/Interface ?

    Hello Folks, Java version 17 has been released around 15 days ago in Sept, 2021. There are a number of features that…

社区洞察

其他会员也浏览了