Java17 sealed classes
Tags: programming, java, java17, beginners
Published: 2023-09-23 10:14:00

## Are Java 17 Sealed Classes a Game-Changer for Inheritance Control?
---
<u>**Introduction:-**</u>

Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17. 
In this complete blog tutorial, we'll see what sealed classes are and why they're useful. But first, let's start with the basic definition of a sealed class.

<u>**Sealed class:-**</u>
- "Sealed class offer a powerful mechanism to restrict the inheritance hierarchy. "

- Which means, When you want's inheritance but not all classes can do it, their should be a limited classes which you want's to be inherited. then you can do this by using sealed class.
- Declaration of sealed class is not much complicated. simply you just need to add the `sealed` keyword to it's declaration.
- After the class declaration add `permits clause` to give the permission to those sub-classes which you want's to be inherited.
- Only permitted classes can have the access to extends the sealed class.


Syntax :-

```java
public sealed class Class_Name permits Subclass1, Subclass2, ... {
    // Class members and methods
}
```
ex :-

```java
sealed class Color permits Red, Green, Blue {
    public void display() {
        System.out.print("This is Color class");
    }
}
```


- in the above example we can see that the ` Color` class is a sealed class that give the permission to `Red` & `Green` classes to extends it. If any other class tries to extend it, then it will give a compiler error.
- A class that extends a sealed class must have either `final` , `sealed` or `non-sealed` keyword in its declaration.

1) <u>**sealed**</u> :-
sealed class can only be extended by it's permitted sub-classes.
```java
sealed class Red extends Color permits R {
    public void display() {
        System.out.println("This is Red class");
    }
}
```
The code defines a sealed class "Red" extending it's super-class "Color" and permitting only class "R", with a display method printing "This is Red class".

```java
final class R extends Red{
    public void display() {
        System.out.println("This is R class");
    }
}
```
The code defines a final class "R" that extends the sealed class "Red" and overrides the display method to print "This is R class".

2) <u>**non-sealed** </u>:-
non-sealed class can be extends by any sub-class. a sealed class cannot prevent its permitted sub-classes from doing this.
```java
non-sealed class Green extends Color {
    public void display() {
        System.out.println("This is Green class");
    }
}
```
The code defines a non-sealed class "Green" that extends it's super-class "Color" and contains a display method which prints "This is Green class".

3) <u>**final** </u>:-
final class can't be inherited further.
```java 
final class Blue extends Color {
    public void display() {
        System.out.println("This is Blue class");
    }
}
```
The code defines a final class "Blue" that extends it's super-class "Color" and contains a display method which prints "This is Blue class". This means that the class "Blue" cannot be subclassed further.

<u>**Code Execution**</u>

```java


public class Main
{
    public static void main(String[] args)
    {   
        Color obj1 = new Red();
        Color obj2 = new Green();
        Color obj3 = new Blue();

        obj1.display();
        obj2.display();
        obj3.display();
        
        Red r = new R();
        r.display();

    }
}

```

<u>Output</u> :- 
```
This is Red class
This is Green class
This is Blue class
This is R class
```
<u>**features**</u>

1. Sealed classes helps in creating a finite set of classes in inheritance.

2. Sealed classes specify which classes can extend them, providing a clear hierarchy.

3. Sealed classes ensure that only permitted sub-classes are created, avoiding unexpected behavior.

4. Useful for creating frameworks, allowing developers to dictate which classes users can extend.

5. Especially beneficial in large projects, it prevents unintentional changes to important classes.

_Note:-_
_- Sealed class must specify it's permitted sub-class using permits clause._
_- Sealed class must have sub-classes._
_- Only the listed sub-classes are allowed to extend the sealed class._
_- Permitted sub-classes must be in same package._
_- Permitted sub-classes should extends their sealed super class directly._

**conclusion**

sealed classes in Java provide a powerful tool for controlling class hierarchies. They allow us to explicitly specify which classes can extend a particular class, offering a clear and organized structure for our code. This feature is valuable in preventing unintended subclasses and maintaining code integrity.



Thanks for reading! Hopefully, this article helps you to understand sealed class in Java17. Please submit any suggestions or comments you may have in the comment section of this article and don't forget to hit the &#x2764;&#xfe0f; like. 
Thank you.&#x1F600;

[sealed interface](https://dev.to/birajdar/can-sealed-interfaces-in-java-improve-code-security-828)