...

Cloud Computing - SCALA

Back to Course

Lesson Description


Lession - #665 SCALA CLASSES AND OBJECTS


This section takes you through how to use classes and items in Scala programming. A class is a blueprint for objects. When you characterize a class, you can make objects from the class diagram with the catchphrase new. Through the article you can utilize all functionalities of the characterized class.

The following diagram shows the class and object by taking an illustration of class student, which contains the member variable (name and roll no>
and member methods (setName(>
and setRollNo(>
>
. At last all are individuals from the class. Class is a blue print and objects are genuine here. In the accompanying chart, Student is a class and Harini, John, and Maria are the objects of Student class, those are having name and roll-number.

Basic Class

Following is a simple syntsx to characterize an basic class in Scala. This class characterizes two factors x and y and a technique: move, which doesn't return a worth. Class factors are called, fields of the class and techniques are called class strategies.

The class name fills in as a class constructor which can take various boundaries. The above code characterizes two constructor contentions, xc and yc; they are both noticeable in the entire body of the class.
Syntax


class Point(xc: Int, yc: Int>
{ var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int>
{ x = x + dx y = y + dy println ("Point x location : " + x>
; println ("Point y location : " + y>
; } }

As mentioned earlier in this chapter, you can create objects using a keyword new and then you can access class fields and methods as shown below in the example − Example


import java.io._

class Point(val xc: Int, val yc: Int>
{ var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int>
{ x = x + dx y = y + dy println ("Point x location : " + x>
; println ("Point y location : " + y>
; } } object Demo { def main(args: Array[String]>
{ val pt = new Point(10, 20>
; // Move to a new location pt.move(10, 10>
; } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.