Google News
logo
Scala - Interview Questions
What are different types of constructors used in Scala?
The constructors are responsible for initializing the state of the object. In the same way as methods, constructors also consist of statements/instructions that are executed when an object is created.

Constructors are used in Scala to create instances of classes. Scala has two types of constructors :  

Primary Constructor : Scala programs often contain only one constructor, known as the primary constructor. It is not necessary to create a constructor explicitly since the primary constructor and the class shares the same body.

Syntax :   
 class class_name(Parameter_list)
{
// Statements...
} ​

Auxiliary Constructor : Auxiliary constructors are the constructors in a Scala program other than a primary constructor. A program may contain any number of auxiliary constructors, but only one primary constructor.

Syntax :
 def this(......)​
Advertisement