Home » Scala language

Scala constructors with examples

Constructors are the methods that are called at the time of object initialization. In this tutorial on Scala constructors, we will learn about Scala objects in detail with examples.
Submitted by Shivang Yadav, on September 04, 2019

Scala constructors

A constructor is a function that is called at the time of initialization of objects. A constructor is just like a method of a class that contains instructions to be executed. The constructor has the same name as the class and is called when the object of the class is initialized.

Scala programming language defines two types of constructors,

  1. Primary constructor
  2. Auxiliary constructor

1) Primary constructor

Also known as default constructor, a primary constructor is created when no other constructor is defined. It has the same name as the class. It is defined by default in Scala.

Syntax:

    class class_name(parameter list){
	    // statements to be executed...
    }

Example:

class student(Sname: String, Ssubject: String, percentage: Int) 
{ 
	def result() 
	{ 
		println("Student name: " + Sname); 
		println("Subject taken: " + Ssubject); 
		println("Percentage obtained:" + percentage); 
	} 
} 
object MyObject 
{ 
	def main(args: Array[String]) 
	{ 
		var obj = new student("Ramesh", "Maths", 78); 
		obj.result(); 
	} 
} 

Output

Student name: Ramesh
Subject taken: Maths
Percentage obtained: 78

2) Auxiliary constructor

In a Scala class, programmers are allowed to create multiple constructors for a single class. But there can be only one primary constructor of a class. Constructor that is explicitly defined by the programmer other than the primary is known as an auxiliary constructor.

Syntax:

    def this(parameter list){
	    //code to be executed
    } 

There can be multiple auxiliary constructors which can be differentiated by their parameter list. In the definition of any auxiliary constructor, the first statement should be a call to the primary constructor or another auxiliary constructor which is defined before the calling constructor using "this" keyword.

Example:

class Student( Sname: String, sub: String) 
{ 
	var percentage: Float = 0.0f; 
	def display() 
	{ 
		println("Student name: " + Sname); 
		println("Stream : " + sub); 
		println("percentage: " + percentage); 
		
	} 
	def this(Sname: String, sub: String, no:Int) 
	{ 
	    this(Sname, sub) 
		this.percentage = no/5 
	} 
} 

object Main 
{ 
	def main(args: Array[String]) 
	{ 
		var obj = new Student("kirti", "biology", 345); 
		obj.display(); 
	} 
} 

Output

Student name: kirti
Stream : biology
Percentage: 69.0





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.