简体   繁体   中英

Abstract and Final class defined on the same Scala Class

I know that Scala allows both abstract and final on the same class.
What is the benefit of this? Example:

final abstract class Parent { ... }

The above code compiles fine. When comparing this to Java, it is not allowed. Also, is there any logic in defining both?

abstract forbids instantiation ( new ...), whereas final forbids subclassing
( new Class { ... } ), so they both serve a different purpose here.

An example class like this in Scala is Int , which is represented by the primitive integer type of the JVM. Because it's primitive type on runtime and the fact that the JVM doesn't allow adding new primitives, there is no legal way to extend this type.
Also there is no way to instantiate it other than by giving a literal (eg val value: Int = 5 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM