In Scala, what is the difference between
val a = 1
and
final val fa = 1
The difference between val and var is that val makes a variable immutable — like final in Java — and var makes a variable mutable.
In Scala, Final is a keyword and used to impose restriction on super class or parent class through various ways. We can use final keyword along with variables, methods and classes.
Differences between var and val keywords in Scala Variable defined are mutable variables. Variables defined are immutable variables. Values are not constant for var variables. Values are constant for val variables.
In short: No, you cannot.
final
members cannot be overridden, say, in a sub-class or trait.
Legal:
class A { val a = 1 } class B extends A { override val a = 2 }
Illegal:
class A { final val a = 1 } class B extends A { override val a = 2 }
You'll get an error such as this:
:9: error: overriding value a in class A of type Int(1);
value a cannot override final member
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With