Constructors in Scala
2 types - Primary and Auxiliary
Primary Constructor
//Primary constructor to accept 2 integer variable and compute the sum
class ConstructorTest(val num1:Int,val num2:Int){
var sum = num1+num2
println(s"Sum of $num1 and $num2 = $sum")
}
- It is part of the class body itself
- Variables are defined with the class body
- All statements within the class are considered to be part of the primary constructor
- Compiler converts the variables to fields and calls getter method for val (Immutable) and setter and getter for var (Mutable)
The same class in java would be
class ConstructorTest{
public final int num1;
public final int num2;
public void ConstructorTest(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
}
public int sum(int num1, int num2){
int sum = num1+num2;
println("Sum of " + num1 + " + " num2 + " = " + sum);
}
}
So many lines of code in Java, is implemented in just 3 lines of code in Scala using Primary Constructor
Auxiliary Constructor
These are same as Java except that
- always named as 'this'
- The first line of code within an auxiliary constructor has to call either another auxiliary constructor or the primary one. This ensures that the object is going through the whole process of initialization.
- Examples below
//Auxiliary constructor1 to accept 3 integer variable and compute the sum of 2 integers using primary constructor and multiply the result by 3rd Integer
def this(num1:Int, num2:Int, num3:Int){
this(num1,num2) //calls the primary constructor
product = num3*sum;
println(s"Product of ($num1 and $num2)*$num3 = $product")
}
//Auxiliary constructor2 to accept 4 integer variable and compute the result of 3 integers using auxiliary constructor1 and divide the result by 4th Integer
def this(num1:Int, num2:Int, num3:Int, num4:Int){
this(num1,num2,num3)
division = product/num4
println(s"Division of $product/$num4 = $division")
}
Object creation for the above class would simply be
object ConstructorObj extends App{
var obj = new ConstructorTest(10,5,2,3)
println(obj)
}
Output of the executing the ConstrutorObj is
Constructor Test
***********************
Sum of 10 and 5 = 15
Product of (10 and 5)*2 = 30
Division of 30/3 = 10
Result: sum=15, product=30, division=10
Complete code for a sample demo of Primary and Auxiliary constructors can be found here
2 types - Primary and Auxiliary
Primary Constructor
//Primary constructor to accept 2 integer variable and compute the sum
class ConstructorTest(val num1:Int,val num2:Int){
var sum = num1+num2
println(s"Sum of $num1 and $num2 = $sum")
}
- It is part of the class body itself
- Variables are defined with the class body
- All statements within the class are considered to be part of the primary constructor
- Compiler converts the variables to fields and calls getter method for val (Immutable) and setter and getter for var (Mutable)
The same class in java would be
class ConstructorTest{
public final int num1;
public final int num2;
public void ConstructorTest(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
}
public int sum(int num1, int num2){
int sum = num1+num2;
println("Sum of " + num1 + " + " num2 + " = " + sum);
}
}
So many lines of code in Java, is implemented in just 3 lines of code in Scala using Primary Constructor
Auxiliary Constructor
These are same as Java except that
- always named as 'this'
- The first line of code within an auxiliary constructor has to call either another auxiliary constructor or the primary one. This ensures that the object is going through the whole process of initialization.
- Examples below
//Auxiliary constructor1 to accept 3 integer variable and compute the sum of 2 integers using primary constructor and multiply the result by 3rd Integer
def this(num1:Int, num2:Int, num3:Int){
this(num1,num2) //calls the primary constructor
product = num3*sum;
println(s"Product of ($num1 and $num2)*$num3 = $product")
}
//Auxiliary constructor2 to accept 4 integer variable and compute the result of 3 integers using auxiliary constructor1 and divide the result by 4th Integer
def this(num1:Int, num2:Int, num3:Int, num4:Int){
this(num1,num2,num3)
division = product/num4
println(s"Division of $product/$num4 = $division")
}
Object creation for the above class would simply be
object ConstructorObj extends App{
var obj = new ConstructorTest(10,5,2,3)
println(obj)
}
Output of the executing the ConstrutorObj is
Constructor Test
***********************
Sum of 10 and 5 = 15
Product of (10 and 5)*2 = 30
Division of 30/3 = 10
Result: sum=15, product=30, division=10
Complete code for a sample demo of Primary and Auxiliary constructors can be found here