5

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?

Didier L
  • 18,905
  • 10
  • 61
  • 103
anion
  • 1,516
  • 1
  • 21
  • 35
  • Related: [what does non-sealed mean](https://stackoverflow.com/questions/63972130/what-is-the-difference-between-a-final-and-a-non-sealed-class-in-java-15s-seale) and [a question on the identically-named feature in Scala](https://stackoverflow.com/questions/32199989/what-are-the-differences-between-final-class-and-sealed-class-in-scala/32200128). – Joachim Sauer Sep 25 '20 at 09:48

1 Answers1

23

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.

anion
  • 1,516
  • 1
  • 21
  • 35