Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sealed and final class in Java [duplicate]

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?

like image 242
anion Avatar asked Apr 14 '26 21:04

anion


1 Answers

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.

like image 185
anion Avatar answered Apr 16 '26 10:04

anion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!