0 votes
in Scala Constructs by
What are different types of constructors used in Scala?

1 Answer

0 votes
by

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(......) 

Related questions

0 votes
asked Sep 11, 2022 in Scala Constructs by sharadyadav1986
0 votes
asked Sep 12, 2022 in Scala Constructs by Robin
...