Scala
Constructors
Constructors
are used to initializing the object’s state. Like methods, a constructor also
contains a collection of statements that are executed at the
time of Object creation.
Scala supports
two types of constructors:
Primary
Constructor
Auxiliary
Constructor
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
program to illustrate the creating a primary constructor with parameter-list
class UPM(Uname: String, Cname: String, articles: Int)
{
def
display()
{
println("User
name: " + Uname);
println("Articles
name: " + Cname);
println("Total
articles:" + articles);
}
}
object Main
{
def
main(args: Array[String])
{
//
Creating and initialzing object of UPMclass
var
obj = new UPM("Umesh",
"Constructors", 6655);
obj.display();
}
}
//Output:
User name: Umesh
Articles name:
Constructors
Total articles:6655
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
parameter.
Example:
// Program to illustrate the concept of
default primary constructor
class UPM
{
def
display()
{
println("Welcome
to My Blogs");
}
}
object Main
{
def
main(args: Array[String])
{
// Creating object of UPMclass
var
obj = new UPM();
obj.display();
}
}
//Output:
Welcome to My
Blogs
· 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 UPM private(name: String,
class:Int){
// code starts from here
}
// private
constructor without argument
class UPM private{
// code starts from here
}
In Scala, we
are allowed to give default values in the constructor declaration.
Example:
// Scala
program to illustrate the
// concept of
primary constructor
// Creating
primary construtor with default values
class UPM(val Uname: String = "UmeshP",
val
Cname: String = "Constructors")
{
def
display()
{
println("User name: " + Uname);
println("Chapter
name: " + Cname);
}
}
object Main
{
def
main(args: Array[String])
{
//
Creating object of UPM class
var
obj = new UPM();
obj.display();
}
}
//Output:
User name: UmeshP
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(......)
· 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
program to illustrate the concept of Auxiliary Constructor Primary Constructor
class UPM( Uname: String, Cname: String)
{
var
no: Int = 0;;
def
display()
{
println("User name: " + Uname);
println("Articles
name: " + Cname);
println("Total
articles: " + no);
}
//
Auxiliary Constructor
def
this(Uname: String,
Cname: String, no:Int)
{
//
Invoking primary constructor
this(Uname, Cname)
this.no=no
}
}
object Main
{
def
main(args: Array[String])
{
//
Creating object of UPM class
var
obj = new UPM("Umesh",
"Constructor", 6655);
obj.display();
}
}
//Output:
User name: Umesh
Articles name:
Constructor
Total articles:
6655
No comments:
Post a Comment