Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are sealed classes enforced in Java and, if yes, how?

It is possible to define sealed classes in Scala, which are basically final except if the sub-classing happens in the same file.

It seems that the JVM doesn't allow final class bytecode and subclasses of it.

Considering that there is no "notion" of source file in the bytecode, how is this restriction enforced?

Therefore, how can javac prevent a Scala sealed class from being sub-classed in Java?

like image 915
soc Avatar asked Dec 16 '10 12:12

soc


1 Answers

The restriction is enforced for Scala source by scalac at compile time. The resulting binary class definitions don't have the JVMs final flag set so, as you've probably guessed by now, the sealed restriction will not be enforced by javac when Java sources are compiled against Scala binaries.

like image 137
Miles Sabin Avatar answered Nov 15 '22 19:11

Miles Sabin