Thursday, February 14, 2013

My Stint with SCALA - Classes and Objects

Scala is an Object Oriented Programming language just like Java. That's why, class is the building block of Scala as well.


Class is defined the same way as defined in Java. Keyword class is used to define a class and this keyword is used by instance to refer itself.

Instantiating Class (Constructors)

Scala provides the default no-argument constructor just like Java does for each class. Custom constructors can be defined with class name, which is a simplified form of defining constructors in Java.

class Point(xc: Int, yc: Int) {
  var x: Int = xc;
  var y: Int = yc;

Above example shows that co-ordinates for Point class can be passed to its primary constructor and can be assigned to instance variables directly.

But does that mean we can define only one constructor for a class? No, Scala supports multiple auxiliary constructor but single primary constructor. This essentially means that the constructor defined along with definition of the class is treated as primary constructor while any other constructor defined in class body will be auxiliary constructor. Auxiliary constructor is dependent on primary constructor to define the instance variables. It has to make a call to primary constructor in order to instantiate the class.

class Point(xc: Int, yc: Int) {
  var x: Int = xc;
  var y: Int = yc;
  def this(x: Int){
    this(x, 0)

Above code shows a auxiliary constructor which creates a point on Y -axis by passing a value for X-aixs and 0 for Y-axis.

As auxiliary constructor can only call primary constructor, so what if class is derived from another class. How auxiliary constructor will call the super constructor? It cannot do that directlywhich means it can only call primary constructor but primary constructor can make call to super constructor. That's how auxiliary constructor can make a call to super constructor through primary constructor.

So in Scala there can be only one primary constructor, rest all are dependent on that unlike Java where each constructor is a separate entity.

Validating Constructor Arguments

As primary constructor is not defined as method in Scala, how to validate the arguments passed? Here it is:

class Point(xc: Int, yc: Int) {
  var x: Int = xc;
  var y: Int = yc;
  require((x == -1 || y == -1), "Negative Co-ordinates are not allowed")

As shown above, method require can be used to validate the arguments passed to constructor. in above example if one of the co-ordinate is negative, an Exception will be thrown with message "Negatives Co-ordinates are not allowed".

Visibility Rules

Most of the visibility rules in Scala are same as Java only with some differences like:

  •  If inner class has a private field even outer class cannot see that variable
  • Default visibility in Scala is Public while its Default in Java.
Rest all visibility scopes like private, public, protected are applicable in Scala with same meaning as in Java.

Setters and Getters

Scala creates a public setter method with name <variable name>_= unless field is marked private. Like for class Point fields x and y setters like x_= and y_= will be created by default. Then these fields can be set by calling this method just like point.x_=(100), which is very easy and clean way of setting the instance variables.

Getter can be defined explicity as a method like:

def xVal = x which is equivalent to

def xVal = {
return x;

So everything like curly braces, return statement is optional. Scala identifies that method is returning something by '=' sign and the return type is interpreted by the type of returned variable.

Thursday, February 7, 2013

My Stint with SCALA - 1

In this blog, instead of trying to put everything that Scala can do, i am trying to put what all i have learnt so far in Scala.

After so many years of love with JAVA, its difficult to give same respect to a new language but good news is that SCALA runs on JVM ( though it can run on .NET platform as well ) and it has simplified many concepts of Java, similar to what Java did for C/C++ concepts.

To start with Scala, it can be downloaded from As it runs on JVM, so  Java Runtime environment is a must for Scala.

One downloaded and extracted, add <scala-home>/bin to the PATH. To check if the scala is running type below command in console. Something similar should be the result, depending upon the version which is installed:

Scala Version Test

All Scala files are stored with .scala format. Unlike Java, a file name can be different than the class name.
So lets start with the first Hello World ! program in Scala.


object HelloWorld {
  def main(args : Array[String]){
    println("HEllo World!!")


Above code looks somewhat similar to HelloWorld in Java. Lets see the details,

First of all, instead of defining a class ( which is the building block in Java ), we are creating a object, object is also a class but a Singleton class in Scala. So we are creating a singleton object. Its worth noticing that defining singleton class in Scala is very easy compared to Java where we have to do much more to make a class Singleton.

After that we are defining main method, which is same as we do in Java to run a standalone class.

In Scala, method definition is recognized with the keyword def, which is followed by name of method. Similar to Java, main method in Scala also takes Array of String as argument.

Input parameter is defined as args: Array[String] which is Scala way to define the variable. args is name of variable and after colon (:) is the type of variable. Notice the way Array of String is defined which is Array[String] as compared to String[] in Java. Array is also a class in class in Scala.

Last we are printing the Hello World using println method, which is similar to System.out.println in Java.

Running Scala

In order to run above class, like Java, Classpath should be set for class. Then run below command to execute:

scala HelloWorld.scala

Output would be as printed in program:

HEllo World!!

Scala share many similarities with Java, still its a different language as one would need to write very less code  for same problem statement.