Updated April 14, 2023
Introduction to Scala Class Constructor
In general programming, language constructors are used to initializing the member variables and object state as well. Constructors should have the same name as the class. Most importantly they are used to initialize the object and execute methods that are required. Scala mainly supports two types of constructor’s i.e. primary and auxiliary constructor. We can also pass parameters inside constructor so at the time of object creation we can initialize them. But remember it should always have the same name as the class name.
Types of Constructors
Below are the types of Constructor:
1. primary Constructor
Syntax:
class name_class(list_of_parameter){
// logic goes here.
}
Below is the example:
Code:
class Demo(name:String, age: Int){
// stat1
// stat2
// stat3
}
2. Auxiliary Constructor
Syntax:
def this(parameter_list)
Below is the example:
defthis(name: String, age: Int)
This constructor we can define using this keyword which contains all other constructors as well. We will discuss them in detail in the next section.
How to Apply Constructor to Class in Scala?
Scala constructors also used to initialize the object state or behavior. Scala provides us two types of constructor which are:
#1. Default Primary Constructor
This constructor is the part of primary constructor only but the difference is that it is introduced by the compiler itself if we do not create any constructor for a class only to support object creation for that class. Below is the example:
Code:
class Student
{
defmessage()
{
println("Student object created !!");
}
}
object Main
{
defmain(args: Array[String])
{
// creating object for Student class.
var s = new Student();
s.message();
}
}
In this type have not created any constructor here but the compiler introduced for us. We are calling the message() method after object creation. But it does not contain any parameter in it.
#2. Primary Constructor
If our Scala class contains only one constructor and this constructor is called the primary constructor or default constructor. If we do not provide any constructor for our class them scala compiler will do this for us to insert a default constructor. This primary constructor contains the same name and body like its class. So if a scala inserts a default constructor for us it means we do not need to create constructor explicitly.
A primary constructor can be a parameterized or non-parameterized depend upon us. In the scala primary constructor shares, the same signature like class the means everything inside the class we define comes under primary constructor only but this does not include them method declaration and definition. If compiler insert constructor for us that would be a zero-argument constructor means with no parameter so if you want to initialize member variable we need to define our own constructor containing the required parameter.
Code:
class Student(name: String, surname: String, age: Int)
{
defgetMessage()
{
println("name of Student: " + name);
println("surname of Student : " + surname);
println("age of Student :" + age);
}
}
object Main
{
defmain(args: Array[String])
{
// creating object
var s = new Student("Demo hello", "bye", 50);
s.getMessage();
}
}
In the above example what we are creating a student class which contains its name, age and surname as a class variable which are getting assign when the object f the class is getting prepared. So in our main method, we are initializing the object with value and calling the getMessage() method to display the information.
#3. Auxiliary Constructor
A scala class can contain any number of auxiliary constructor but the parameter list should be different. We can also call other constructors from auxiliary constructor for this we can use this keyword. Like any other programming language, we have to call the other constructor at the beginning of the constructor statement in other word it should be the first statement.
The class can contain both primary and auxiliary constructors.
By this, we can say that the scala auxiliary constructor is another name for constructor overloading because it works in the same manner. We can specify any number of auxiliary constructors with different parameter lists otherwise it leads to constructor ambiguity.
Code:
class Flower(color:String){
defthis(color:String, name:String)={
this(color)
println(color+" "+name)
}
}
object MainObject{
def main(args:Array[String]){
new Flower("Red")
new Flower("Red","Rose")
}
}
In this example, we have defined two constructors one for initializing color and second for both color and name. Just printing the information from the constructor only.
Examples to Implement Scala Class Constructor
Below are the examples Scala Class Constructor:
Example #1
In this example shows how the default constructor works in scala.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var student = new Student()
student.getInformation()
}
class Student{
defgetInformation(){
println("caliing method ::: ")
println("Object created successfully.!!")
}
}
Output:
Example #2
This example shows how the primary constructor works in the scala.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var student1 = new Student("Aman", 20, "Indore", 001)
var student2 = new Student("Amit", 30, "Indore", 002)
var student3 = new Student("Arun", 22, "Indore", 003)
var student4 = new Student("Anamika", 19, "Indore", 004)
var student5 = new Student("Aditi", 18, "Indore", 005)
student1.getStrudentInfo()
student2.getStrudentInfo()
student3.getStrudentInfo()
student4.getStrudentInfo()
student5.getStrudentInfo()
}
class Student(name: String, age: Int, address: String, Id: Int){
defgetStrudentInfo(){
println("name of the student is :: " +name)
println("age of the student is :: " +age)
println("address of the student is :: " +address)
println("id of the student is :: " +Id)
}
}
Output:
Example #3
Usage of the auxiliary constructor.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var flower1 = new Flower("Rose", "Red", 001)
var flower2 = new Flower("flower 2","yellow", 002)
flower1.getFlowerInfo()
}
class Flower(name: String, color: String){
var Id: Int = 0;;
defthis(name: String, color: String, Id: Int){
this(name, color)
this.Id = Id
}
defgetFlowerInfo(){
println("name of the flower is :: " +name)
println("color of the flower is :: " +color)
println("id of the flower is :: " +Id)
}
}
Output:
Conclusion
Scala constructors also used to create an object. It provides us two types of primary and auxiliary constructors. We can also overload constructor by using the second type of auxiliary constructor. They are basically for maintain the object state without it we cannot proceed further.
Recommended Articles
We hope that this EDUCBA information on “Scala Class Constructor” was beneficial to you. You can view EDUCBA’s recommended articles for more information.