Scala | Auxiliary Constructor - GeeksforGeeks

17 Jun, 2021
Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements (i.e. instructions). Statements are executed at the time of object creation. In 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 Scala class, but a scala class contains only one primary constructor.Auxiliary constructors are defined as methods in the class with the keyword this. We can describe multiple auxiliary constructors, but they must have different parameter lists.Syntax :  
def this(......)
Let’s try to understand Auxiliary constructors with help of some examples. Example #1: Using one Auxiliary Constructor  
Scala
// Scala program to illustrate the// concept of Auxiliary Constructor // Primary constructorclass GFG( Lname: String, Tname: String){    var no: Int = 0;;    def show()    {        println("Language name: " + Lname);        println("Topic name: " + Tname);        println("Total number of articles: " + no);             }         // Auxiliary Constructor    def this(Lname: String, Tname: String, no:Int)    {                 // Invoking primary constructor        this(Lname, Tname)        this.no = no    }} // Creating objectobject Main{    // Main method    def main(args: Array[String])    {                 // Creating object of GFG class        var obj = new GFG("Scala", "Constructor", 4);        obj.show();    }}
Output:  
Language name: Scala
Topic name: Constructor
Total number of articles: 4
In above example, as we can see only one auxiliary constructor is used and primary constructor invoked in that auxiliary constructor. After creating object of GFG class(obj), show() function will be called and print the result.   Example #2: Using more than one Auxiliary Constructor.  
scala
// Scala program to illustrate the// concept of more than concept// Auxiliary Constructor // Primary constructorclass Company{    private var Cname = ""    private var Employee = 0       // Creating function    def show()    {        println("Language name: " + Cname);        println("Total number of employee: " + Employee);    }     // An auxiliary constructor    def this(Cname: String)    {        // Calls primary constructor        this()        this.Cname = Cname    }     // Another auxiliary constructor    def this(Cname: String, Employee: Int)    {          // Calls previous auxiliary constructor        this(Cname)        this.Employee = Employee    }} // Creating objectobject Main{    // Main method    def main(args: Array[String])    {        // Primary constructor        val c1 = new Company        c1.show()                 // First auxiliary constructor        val c2 = new Company("GeeksForGeeks")        c2.show()                 // Second auxiliary constructor        val c3 = new Company("GeeksForGeeks", 42)        c3.show()             }}
Output:  
Language name: 
Total number of employee: 0
Language name: GeeksForGeeks
Total number of employee: 0
Language name: GeeksForGeeks
Total number of employee: 42
In above example, as we can see two auxiliary constructors are created with different parameters. Auxiliary constructor invoked primary constructor and another auxiliary constructor invoked previously defined auxiliary constructor. 
 
In a single class, we are allowed to create one or more than one auxiliary constructors, but they have different signatures or parameter-lists.
Each auxiliary constructor must call one of the previously defined constructors, this would be primary constructor or previous auxiliary constructor.
The invoke constructor may be a primary or previous auxiliary constructor that comes textually before the calling constructor.
The first statement of the auxiliary constructor must contain this keyword.
 
varshagumber28
Scala-Constructor
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Scala Map
Scala Lists
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List contains() method with example
Scala | Arrays
Scala String substring() method with example
How to get the first element of List in Scala
Lambda Expression in Scala
Scala String replace() method with example
Scala Map get() method with example