Search code examples
javasealed-classjava-15

Difference between sealed and final class in Java


I have just read that Java 15 should have sealed classes. jep360 says:

Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.

I thought that this is exactly what a final class does in Java. So now I wonder: What is the difference between a final class and a sealed class?


Solution

  • final class A {...} means that no class is allowed extend A.

    sealed class A permits B {...} means that only B can extend A but no other class is allowed to do that.