What is the difference between declaring a field as val
, lazy val
and object
inside a scala class, as in the following snippet:
One major difference is that val's can be overriden while objects can't.
class C extends B {
override val a1 = new A { def foo = 2 }
override object a2 extends A { def foo = 2 }
}
leads to:
:9: error: overriding object a2 in class B of type object C.this.a2;
object a2 cannot be used here - classes and objects cannot be overridden
override object a2 extends A { def foo = 2 }