final has overloaded values.
This may mean that "cannot be overridden in subclasses", hence final var .
apm@mara :~$ skala -Yoverride-vars Welcome to Scala version 2.11.0-20130811-132927-95a4d6e987 (OpenJDK 64-Bit Server VM, Java 1.7.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> trait Foo { var v = 7 } defined trait Foo scala> trait Bar extends Foo { override var v = 8 } defined trait Bar scala> trait Foo { final var v = 7 } defined trait Foo scala> trait Bar extends Foo { override var v = 8 } <console>:8: error: overriding variable v in class Foo$class of type Int; variable v cannot override final member trait Bar extends Foo { override var v = 8 } ^
final val i = 7 is a constant definition (or compile-time constant), but val i = 7 not, regardless of the access modifier.
This was stated earlier, but 5.2 from the specification:
The last modifier applies to class member definitions and class definitions. The final definition of a class member cannot be overridden in subclasses. The final class cannot be inherited by the template. final is redundant for object definitions. Finite class members or objects are also implicitly final, so the final modifier is usually redundant for them too. Note, however, that the constant definition value (ยง4.1) requires an explicit final modifier, even if they are defined in the final class or object. the ending cannot be applied to incomplete members, and it cannot be combined into one list of modifiers with a sealed one.
and 4.1
The definition of a constant value has the form
final val x = e
where e is a constant expression (ยง6.24). The last modifier should be present and type annotations. References to a constant value of x are themselves regarded as constant expressions; in the generated code they are replaced by the definitions of the right side e.
Edit: Sorry, didnโt notice that you didnโt specifically ask about this. Children get ready for bed, brush their teeth and all this is a little distracted.
source share