Following is a Scala class with constructors. My questions are marked with ****
class Constructors( a:Int, b:Int ) {
def this() =
{
this(4,5)
val s : String = "I want to dance after calling constructor"
//**** Constructors does not take parameters error? What is this compile error?
this(4,5)
}
def this(a:Int, b:Int, c:Int) =
{
//called constructor's definition must precede calling constructor's definition
this(5)
}
def this(d:Int)
// **** no equal to works? def this(d:Int) =
//that means you can have a constructor procedure and not a function
{
this()
}
//A private constructor
private def this(a:String) = this(1)
//**** What does this mean?
private[this] def this(a:Boolean) = this("true")
//Constructors does not return anything, not even Unit (read void)
def this(a:Double):Unit = this(10,20,30)
}
Could you please answer my questions in the **** above? For example Constructors does not take parameters error? What is this compile error?
The auxiliary constructor in Scala is used for constructor overloading and defined as a method using this name. The auxiliary constructor must call either previously defined auxiliary constructor or primary constructor in the first line of its body.
Auxiliary constructors are defined by creating methods named this . Each auxiliary constructor must begin with a call to a previously defined constructor. Each constructor must have a different signature. One constructor calls another constructor with the name this .
There are two types of constructor in Scala – Primary and Auxiliary. Not a special method, a constructor is different in Scala than in Java constructors. The class' body is the primary constructor and the parameter list follows the class name.
The primary constructor of a Scala class is a combination of: The constructor parameters. Methods that are called in the body of the class. Statements and expressions that are executed in the body of the class.
Ans 1:
scala> class Boo(a: Int) {
| def this() = { this(3); println("lol"); this(3) }
| def apply(n: Int) = { println("apply with " + n) }
| }
defined class Boo
scala> new Boo()
lol
apply with 3
res0: Boo = Boo@fdd15b
First this(3)
is a delegation to primary constructor. The second this(3)
invokes this object's apply method i.e. expands to this.apply(3)
. Observe the above example.
Ans 2:
=
is optional in constructor definitions as they don't really return anything. They have different semantics from regular methods.
Ans 3:
private[this]
is called object-private access modifier. An object cannot access other object's private[this]
fields even though they both belong to the same class. Thus it's stricter than private
. Observe the error below:
scala> class Boo(private val a: Int, private[this] val b: Int) {
| def foo() {
| println((this.a, this.b))
| }
| }
defined class Boo
scala> new Boo(2, 3).foo()
(2,3)
scala> class Boo(private val a: Int, private[this] val b: Int) {
| def foo(that: Boo) {
| println((this.a, this.b))
| println((that.a, that.b))
| }
| }
<console>:17: error: value b is not a member of Boo
println((that.a, that.b))
^
Ans 4:
Same as Ans 2.
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