Skip to content
Related Articles

Related Articles

Improve Article

Scala Constructors

  • Difficulty Level : Expert
  • Last Updated : 11 Aug, 2021
Geek Week

Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation
Scala supports two types of constructors: 

Primary Constructor

When our Scala program contains only one constructor, then that constructor is known as a primary constructor. The primary constructor and the class share the same body, means we need not to create a constructor explicitly. 

Syntax:  

class class_name(Parameter_list){
// Statements...
}

Important points:  

  • In the above syntax, the primary constructor and the class share the same body so, anything defined in the body of the class except method declaration is the part of the primary constructor. 
    Example: 

Scala




// Scala program to illustrate the
// concept of primary constructor
 
// Creating a primary constructor
// with parameter-list
class GFG(Aname: String, Cname: String, Particle: Int)
{
    def display()
    {
        println("Author name: " + Aname);
        println("Chapter name: " + Cname);
        println("Total published articles:" + Particle);
    }
}
 
object Main
{
    def main(args: Array[String])
    {
         
        // Creating and initializing
        // object of GFG class
        var obj = new GFG("Ankita", "Constructors", 145);
        obj.display();
    }
}

Output: 



Author name: Ankita
Chapter name: Constructors
Total published articles:145
  • The primary constructor may contain zero or more parameters.
  • If we do not create a constructor in our Scala program, then the compiler will automatically create a primary constructor when we create an object of your class, this constructor is known as a default primary constructor. It does not contain any parameters. 
    Example: 

Scala




// Scala program to illustrate the
// concept of default primary constructor
 
class GFG
{
    def display()
    {
        println("Welcome to Geeksforgeeks");
    }
}
 
object Main
{
    def main(args: Array[String])
    {
         
        // Creating object of GFG class
        var obj = new GFG();
        obj.display();
    }
}

Output: 

Welcome to Geeksforgeeks
  • If the parameters in the constructor parameter-list are declared using var, then the value of the fields may change. And Scala also generates getter and setter methods for that field. 
     
  • If the parameters in the constructor parameter-list are declared using val, then the value of the fields cannot change. And Scala also generates a getter method for that field.
     
  • If the parameters in the constructor parameter-list are declared without using val or var, then the visibility of the field is very restricted. And Scala does not generate any getter and setter methods for that field.
     
  • If the parameters in the constructor parameter-list are declared using private val or var, then it prevents from generating any getter and setter methods for that field. So, these fields can be accessed by the members of that class. 
     
  • In Scala, only a primary constructor is allowed to invoke a superclass constructor.
  • In Scala, we are allowed to make a primary constructor private by using a private keyword in between the class name and the constructor parameter-list. 
    Syntax: 
// private constructor with two argument
class GFG private(name: String, class:Int){
// code..
}

// private constructor without argument
class GFG private{
// code...
}
  • In Scala, we are allowed to give default values in the constructor declaration. 
    Example: 

Scala




// Scala program to illustrate the
// concept of primary constructor
 
// Creating primary constructor with default values
class GFG(val Aname: String = "Ankita",
          val Cname: String = "Constructors")
{
    def display()
    {
        println("Author name: " + Aname);
        println("Chapter name: " + Cname);
         
    }
}
 
object Main
{
    def main(args: Array[String])
    {
        // Creating object of GFG class
        var obj = new GFG();
        obj.display();
    }
}

Output: 

Author name: Ankita
Chapter name: Constructors 

Auxiliary Constructor

In a Scala program, the constructors other than the primary constructor are known as auxiliary constructors. we are allowed to create any number of auxiliary constructors in our program, but a program contains only one primary constructor. 

Syntax:  

def this(......)

Important points:  

  • In a single program, we are allowed to create multiple auxiliary constructors, but they have different signatures or parameter-lists.
  • Every auxiliary constructor must call one of the previously defined constructors.
  • The invoke constructor may be a primary or another auxiliary constructor that comes textually before the calling constructor.
  • The first statement of the auxiliary constructor must contain the constructor call using this.

Example:  

Scala




// Scala program to illustrate the
// concept of Auxiliary Constructor
 
// Primary constructor
class GFG( Aname: String, Cname: String)
{
    var no: Int = 0;;
    def display()
    {
        println("Author name: " + Aname);
        println("Chapter name: " + Cname);
        println("Total number of articles: " + no);
         
    }
     
    // Auxiliary Constructor
    def this(Aname: String, Cname: String, no:Int)
    {
         
        // Invoking primary constructor
        this(Aname, Cname)
        this.no=no
    }
}
 
object Main
{
    def main(args: Array[String])
    {
         
        // Creating object of GFG class
        var obj = new GFG("Anya", "Constructor", 34);
        obj.display();
    }
}

Output: 

Author name: Anya
Chapter name: Constructor
Total number of articles: 34

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :