Declaring abstract "var" members

It's a bad practice to declare abstract vars in abstract classes or traits. Do not do this:
trait Foo {
  var value: String
Instead, prefer to declare abstract things as def:
trait Foo {
  def value: String

// can then be overridden as anything
class Bar(val value: String) extends Foo
The reason has to do with the imposed restriction - a var can only be overridden with a var. The way to allow freedom to choose on inheritance is to use def for abstract members. And why would you impose the restriction to use a var on those that inherit from your interface. def is generic so use it instead.
Read more.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala snippets
Sign Up Now
or Subscribe for future quizzes