Constructors

The concept and the usage of constructors in Scala are a little different than what they are in C# or Java. There are two types of constructors in Scala - primary and auxiliary constructors. The primary constructor is the class's body, and it's parameter list appears right after the class name.

For example, the following code segment describes the way to use the primary constructor in Scala:

class Animal (animalName:String, animalAge:Int) {
def getAnimalName () : String = {
animalName
}
def getAnimalAge () : Int = {
animalAge
}
}

Now, to use the preceding constructor, this implementation is similar to the previous one, except there are no setters and getters. Instead, we can get the animal name and age, as here:

object RunAnimalExample extends App{
val animalObj = new animal("Cat",-1)
println(animalObj.getAnimalName)
println(animalObj.getAnimalAge)
}

Parameters are given in the class definition time to represent constructors. If we declare a constructor, then we cannot create a class without providing the default values of the parameters that are specified in the constructor. Moreover, Scala allows the instantiation of an object without providing the necessary parameters to its constructor: this happens when all constructor arguments have a default value defined.

Although there is a constraint for using the auxiliary constructors, we are free to add as many additional auxiliary constructors as we want. An auxiliary constructor must, on the first line of its body, call either another auxiliary constructor that has been declared before it, or the primary constructor. To obey this rule, each auxiliary constructor will, either directly or indirectly, end up invoking the primary constructor.

For example, the following code segment demonstrates the use of the auxiliary constructor in Scala:

class Hello(primaryMessage: String, secondaryMessage: String) {
def this(primaryMessage: String) = this(primaryMessage, "")
// auxilary constructor
def sayHello() = println(primaryMessage + secondaryMessage)
}
object Constructors {
def main(args: Array[String]): Unit = {
val hello = new Hello("Hello world!", " I'm in a trouble,
please help me out.")
hello.sayHello()
}
}

In the earlier setting, we included a secondary (that is, 2nd) message in the primary constructor. The primary constructor will instantiate a new Hello object. Method sayHello() will print the concatenated message.

Auxiliary constructors: In Scala, defining one or more auxiliary constructors for a Scala class gives the consumers of the class different ways to create object instances. Define the auxiliary constructors as methods in the class with the name this. You can define multiple auxiliary constructors, but they must have different signatures (parameter lists). Also, each constructor must call one of the previously defined constructors.

Now let's peep into another important but relatively new concept in Scala, called traits. We will discuss this in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32