Sunday, 17 December 2017

Introduction to Scala - III

We continue with more Scala on the topic of classes in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. Classes in Scala are similar to classes in any other language. In this post, we will look at the way classes are declared and instantiated in programs.

A class can be defined as shown below:

class FirstClass

val FirstClassInstance1 = new  FirstClass

The output is shown below:

scala> class FirstClass
defined class FirstClass

scala> val FirstClassInstance1 = new  FirstClass
FirstClassInstance1: FirstClass = FirstClass@dca2615


class FirstClass declares a class of FirstClass.

val FirstClassInstance1 = new  FirstClass with the new keyword creates an instance of class, FirstClass and assigns the newly created instance to val, FirstClassInstance1. Like in Java, default constructor of FirstClass is invoked as there are no arguments. dca2615 is the address of the instance in the memory of computer.

We see a constructor in the next example:

class SecondClass(val x:Double,val y:Boolean){
println("x is " + x + " and y is "+ y)
}

val SecondClassInstance1 = new SecondClass(1.0,true)


The results are shown below:

scala> class SecondClass(val x:Double,val y:Boolean){
     | println("x is " + x + " and y is "+ y)
     | }
defined class SecondClass

scala> val SecondClassInstance1 = new SecondClass(1.0,true)
x is 1.0 and y is true
SecondClassInstance1: SecondClass = SecondClass@3b55dd15


We can set default values on the parameters. In the below example, we have set a default value for parameter, x only.

class ThirdClass(val x:Double = 10.0,val y:Boolean){
println("x is " + x + " and y is "+ y)
}

val ThirdClassInstance1 = new ThirdClass(y = true)


The results are shown below:

 scala> class ThirdClass(val x:Double = 10.0,val y:Boolean){
     | println("x is " + x + " and y is "+ y)
     | }
defined class ThirdClass

scala> val ThirdClassInstance1 = new ThirdClass(y = true)
x is 10.0 and y is true
ThirdClassInstance1: ThirdClass = ThirdClass@4dd90166


In the next example, we add a private instance variable and a couple of methods to get the value and set the value of the private instance variable.

class FourthClass {
  private var x:Long = 0
  def Setx(newValue: Long): Unit = {
    x = newValue
  }
  def Getx = x
}

val FourthClassInstance1 = new FourthClass
FourthClassInstance1.Getx
FourthClassInstance1.Setx(100L)
FourthClassInstance1.Getx


The results are shown below:

scala> class FourthClass {
     |   private var x:Long = 0
     |   def Setx(newValue: Long): Unit = {
     |     x = newValue
     |   }
     |   def Getx = x
     | }
defined class FourthClass

scala> val FourthClassInstance1 = new FourthClass
FourthClassInstance1: FourthClass = FourthClass@72543547

scala> FourthClassInstance1.Getx
res9: Long = 0

scala> FourthClassInstance1.Setx(100L)

scala> FourthClassInstance1.Getx
res11: Long = 100


If we try to access the private variable x directly, we get below error:

scala> FourthClassInstance1.x
<console>:14: error: variable x in class FourthClass cannot be accessed in FourthClass
       FourthClassInstance1.x
                            ^


With this we conclude the discussion on Classes in Scala.