Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala - Auxiliary Constructor - Testingpool

Scala – Auxiliary Constructor

In the previous post, we have learnt primary constructor in scala. Now, we will learn about Auxiliary constructor in scala.

In scala, constructor other than primary constructor are called Auxiliary Constructor or secondary constructor. We can have multiple constructors in a class and there are few points below to understand them better.

  • Auxiliary constructors are the methods in the class which can be created with def this(…). That means one constructor calls another constructor with method named this.
  • There can be multiple Auxiliary constructors but all should have the different signature (parameter list). It can have zero or one or more parameters.
  • Each constructor must call one of the previously defined constructor and placed it as first line of its body.
  • Auxiliary constructor can call either primary constructor or another auxiliary constructor by using this name.Hence, every auxiliary constructor invokes directly or indirectly to a primary constructor.

Example 1 – Create a primary constructor and an auxiliary constructor

Let’s understand this with an example where we define a class having a primary constructor and a zero arguments auxiliary constructor.

  //Primary Constructor
class Employee(firstName:String,middleName:String,lastName:String){
  println("Primary Constructor displays name - " + firstName +" " + middleName + " " + lastName)

  //zero argument auxiliary constructor
  def this(){
    this("Default FN ", "Default MN" , " Default LN")
    println("Zero parameters auxiliary constructor")
  }
}

Now, create an object with no parameter and it should call auxiliary constructor which will make a call to primary constructor. In other words, first primary constructor will be executed and then auxiliary constructor.

object ConstructorExample extends App{
  val emp3 = new Employee                     //Calling 0 parameter auxiliary constructor
}
Output: 
Primary Constructor displays name - Default FN  Default MN  Default LN
Zero parameters auxiliary constructor

If object is created with parameters, then only primary constructor will be called.


object ConstructorExample extends App{
  val emp3 = new Employee("Shekhar","","Sharma")                     //Calling primary constructor
}
Output:
Primary Constructor displays name - Shekhar  Sharma

Example 2 – Create a primary constructor and multiple auxiliary constructors.

  //Primary Constructor
class Employee(firstName:String,middleName:String,lastName:String){
  println("Primary Constructor displays name - " + firstName +" " + middleName + " " + lastName)


    //zero arguments auxiliary constructor
  def this(){
    this("Default FN ", "Default MN" , " Default LN")
    println("Zero parameters auxiliary constructor")
  }

    //2 arguments auxiliary constructor
    def this(FN:String,LN:String){
      this()
      println("2 parameters auxiliary constructor - " + FN +" "+LN)
    }
}

Create an object with 2 parameters.

object ConstructorExample extends App{
  val emp3 = new Employee("John","Hough")                     //Calling 2 arguments constructor
}
Output: 
Primary Constructor displays name - Default FN  Default MN  Default LN
Zero parameters auxiliary constructor
2 parameters auxiliary constructor - John Hough

In the above example, first 2 arguments constructor will be called which is having zero argument constructor as its first line and it will call primary constructor. So, it will have execution sequence as Primary constructor –> Zero arguments constructor –> 2 arguments constructor.

So far, we have discussed the constructor overloading with auxiliary constructors. Is it possible to perform constructor overloading without auxiliary constructor or by using only primary constructor?

Answer is Yes, it is possible as Scala has introduced a new feature called as “Scala’s Default Arguments concept” from version 2.9.

Example: Let’s redefine the Employee class with default parameters as given below.

  //Primary Constructor with default parameters 
class Employee(firstName:String = "John",middleName:String = "M",lastName:String = "Hough"){
  println("Primary Constructor displays name - " + firstName +" " + middleName + " " + lastName)

}

Test1 – Create an object without arguments.

object ConstructorExample extends App{
  val emp = new Employee
}
Output:
Primary Constructor displays name - John M Hough

Test 2 – Change the first name.

object ConstructorExample extends App{
  val emp = new Employee("Ryan")
}
Output:
Primary Constructor displays name - Ryan M Hough

Test 3 – Change the last name.


object ConstructorExample extends App{
  val emp = new Employee(lastName="Thomas")
}
Output:
Primary Constructor displays name - John M Thomas
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...