Home » Scala language

Classes and Objects in Scala

In this tutorial, we are going to learn about classes and objects in Scala programming language. The object is a very important concept of programming and mastering it is important to become a good programmer.
Submitted by Shivang Yadav, on June 17, 2019

Classes in Scala

A class is a blueprint for objects. It contains the definition of all the members of the class. There are two types of members of the class in Scala,

  1. Fields: the variables in Scala that are used to define data in the class.
  2. Methods: The functions of a class that are used to manipulate the fields of the class and do some stuff related to the functioning of a class.

Usage of a class member to the outer worlds is limited and can be excessed by only two ways,

  1. Inheritance: Property by which members of one class are used by child class which inherits it.
  2. Objects: It is creating instances of a class to use its members.

Example of a class

We will use the classical student class for the explanation of class creation in Scala. Here, we have a class student, with fields: roll no, name, and percentage. There are methods to this class: getpercentage(), printresult().

The blueprint is shown in the below figure...

Syntax of class in Scala:

Class student{
	// Class Variables
	var rollno;
	var name : string;
	var percentage; 

	//Class methods…
	def getpercentage(int percent){
		percentage = percent;
	}
	
	def printresult(){
		print("Roll number : " + rollno);
		print("\nName  : "+ name);
		print("\nHas scored " + percentage + "% and is ");
		if(percentage > 40)
			print("passed")
		else 
			print("failed")
	}
}

Syntax explanation:

The about is a code snippet to declare a class in Scala,

First, the class is declared keyword class is used to create a class followed by the name of the class. Next is the definition of the class members, the class contains three members all are public (because of var declaration). It also contains two member functions(methods) that are declared using def keyword (no need of return type), followed by the name of the function and then within the "(" are the arguments that are passed when the function is called.

classes in scala

Primary Constructor in Scala

There is a new declaration of class in Scala, and you will see it very it is more efficient than the classical one.

It is the use of primary constructor definition in Scala.

    class student (var rlno , var stname){
	    var rollno = rlno;
	    var name = stname; 
    }

Explanation:

Here the class body is acting as a constructor and is used to initialize the values of fields.

This is all about classes in Scala we will learn about objects, their creation, and usage in the next tutorial.






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.