Absolutely not, and your example demonstrates why. You've introduced a mutable variable value
that may or may not be initialised - depending on the exact constructor used.
This is a potential source for a great many problems, and so Scala made the decision that all object creation should ultimately be directed via the primary constructor, this ensuring consistent initialisation.
If you want value
to have a default value, then you can specify it as a default parameter (in 2.8+):
abstract class Father(val value : Int = 0)
or you can use the auxiluary constructor to achieve the same effect in Scala 2.7:
abstract class Father(val value : Int) {
def this() = this(0)
}
With Father
defined in either of the above ways, the following definitions of child are both valid:
class Child(v:Int) extends Father(v)
class Child extends Father()
You can also make value
a var if you absolutely have to, but I strongly advise against it.
If the semantics of value
mean that it's valid to not be initialised, then the correct Scala idiom is to declare it as Option[Int]
:
abstract class Father(val value : Option[Int] = Some(0))