Scala Constructor – 2 Popular Types of Constructors in Scala

1. Objective

In our previous tutorial, we studied Scala trait Mixins and now we are going to discuss Scala constructor and types of constructor in Scala Programming Language: Primary Constructor and Auxiliary Constructors in Scala. At last, we will cover Constructor Overloading in Scala.

So, let’s begin Scala Constructor Tutorial.

Scala Constructor - 2 Popular Types of Constructors in Scala

Scala Constructor – 2 Popular Types of Constructors in Scala

2. Scala Constructor

Scala constructor is used for creating an instance of a class. 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 following, then, is the default primary constructor.
Let’s see Scala Currying Function – Example & Partially Applied Function

3. Default Primary Constructor in Scala

scala>class Vehicle{
| println("I am in the default constructor")
| }
defined class Vehicle
scala> val v=new Vehicle()

I am in the default constructor
v: Vehicle = Vehicle@78288f83
Here, the class body has one print statement. This is the code in the default constructor. We could also have defined it without parentheses:

scala> val v=new Vehicle

I am in the default constructor
v: Vehicle = Vehicle@7c751692

4. A Primary Constructor in Scala

A class in Scala may have only one primary constructor. Such a constructor may have zero or more parameters.

scala> class Vehicle(model:String){
| println("I am a "+model+".")
| }
defined class Vehicle
scala> val v=new Vehicle("Brio")

I am a Brio.
v: Vehicle = Vehicle@42cfd794
Read about Scala Method Overloading with Example
Here, a model is a val; we cannot reassign it. Take a look:

scala> class Vehicle(model:String){
| println("I am a "+model+".")
| model="Verna"
| println("I am now a "+model+".")
| }
<console>:13: error: reassignment to val
model="Verna"
^

But we can declare it to be a var:

scala> class Vehicle(var model:String){
| println("I am a "+model+".")
| model="Verna"
| println("I am now a "+model+".")
| }
defined class Vehicle
scala> val v=new Vehicle("Brio")

I am a Brio.
I am now a Verna.
v: Vehicle = Vehicle@11174bf
In this case, the compiler generates setter and getter methods for the var.
Take another example:

object Main extends App{
class Vehicle(model:String,color:String){
println("I am a "+color+" "+model+".")
}
var v=new Vehicle("Verna","red")
}

In the command prompt, using the Java Class File Disassembler:
C:\Users\lifei\Desktop>javap Main.class
Compiled from “prim.scala”

public final class Main {
public static void main(java.lang.String[]);
public static void delayedInit(scala.Function0<scala.runtime.BoxedUnit>);
public static void delayedEndpoint$Main$1();
public static long executionStart();
public static void v_$eq(Main$Vehicle);
public static Main$Vehicle v();
}

Let’s discuss Scala Object – Scala Singleton & Scala Companion Object

5. Auxiliary Constructors in Scala

A class may have any number of auxiliary constructors. You must make sure that it must make a call to another auxiliary constructor or to the primary constructor in the first line of its body. Hence, each auxiliary constructor directly or indirectly invokes the primary constructor.
Let’s take an example.

scala> class Vehicle(model:String,color:String){
| var year:Int=2000
| def show(){
| println("I am a "+color+" "+model+" from "+year)
| }
| def this(model:String,color:String,year:Int){
| this(model,color) //Call to the primary constructor
| this.year=year
| }
| }
defined class Vehicle
scala> var v=new Vehicle("Verna","red",2015)

v: Vehicle = Vehicle@18dd7767

scala> v.show()

I am a red Verna from 2015

scala> var v1=new Vehicle("Verna","black")

v1: Vehicle = Vehicle@b2d8dcd

scala> v1.show()

I am a black Verna from 2000
Do you know about Scala File i/o: Open, Read and Write a File in Scala

Scala Quiz

6. Scala Constructor Overloading

Finally, let us take a look at how to overload constructors.

scala> class Vehicle(model:String){
| def this(model:String,color:String)={
| this(model)
| println("I am a "+color+" "+model)
| }
| println(model)
| }
defined class Vehicle
scala> new Vehicle("Prius")

Prius

res2: Vehicle = Vehicle@421d7900
scala> new Vehicle("Prius","silver")

Prius
I am a silver Prius
res3: Vehicle = Vehicle@42ea14b8
This lets us make a call to the constructor that suits the number of arguments we provide.
Have a Look at Scala String Method with Syntax and Method
So, this was all about Scala Constructor. Hope you like our explanation of types of Constructor in Scala.

7. Conclusion

Hence, in this Scala constructor tutorial, we studied types of constructor: Primary Constructor and Auxiliary Constructors in Scala. In Addition, we will cover constructor overloading in Scala Programming Language.
Related Article- Scala Data Types with Examples 
For reference 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.