Saturday, January 17, 2015
Like any object oriented programming language Scala allows to define classes which is the code template for creating objects. This article provides a quick introduction to work with Scala classes.

Scala Classes, Fields and Methods

Scala Class is the extensible static code template that can be initiated into objects at run time. Class definition in Scala can take parameters like constructor arguments. 

Scala Fields are member variables that can be accessed anywhere in the class and provide the initial values. 

Scala Methods are member functions that provide the class behavior.

A simple example of a Scala class. Here employeeId and employeeName are constructor fields, field1 is the class field and print is the class method.

class Employee (var employeeId : Int, var employeeName : String) {

  println("Initializing employee ...")
  var field1: Int = 0

  def print() = {
    println(employeeId + " " + employeeName)
  }
  
  println("More initialization ...")
}

To create an instance of a Scala class we use new primitive. In this example, we create an instance of class Employee and invoke a method on the instance.

object EmployeeTest {
  def main(args:Array[String]) {
    val emp = new Employee(5, "John")
    emp.print
  }
}

Scala Class Constructor

The above Employee example produces the following output.

Initializing employee ...
More initialization ...
5 John

In Scala the primary constructor is different from other programming languages like Java/ C++. The constructor fields, statements/ expressions/ methods in the body of the class form the Scala primary class constructor.

For constructor fields and class fields defined as var (e.g. employeeId, employeeName, field1) Scala automatically generates getters and setters which helps to make the Scala code really concise when compared to Java. If the fields are declared as val Scala generates only the getter method.

For example, getters and setters of a constructor field can be accessed as below.

object EmployeeTest {
  def main(args:Array[String]) {
    val emp = new Employee(5, "John")
    emp.print
    emp.employeeId = 6
    println(emp.employeeId)   
  }
}

Multiple Class Constructors

Classes need to have flexibility to provide various approaches to create objects. This is achieved in Scala using auxiliary constructors. There could be multiple auxiliary constructors and each of them need to have a different signature and has to call one of the previously defined constructors.

Let us look at a variation of the Employee class to understand auxiliary constructors. Auxiliary constructors are defined by creating methods named this. Also one constructor calls another constructor using name this.

class Employee1 (var id : Int, var firstName : String, 
    var lastName: String) {

  def this(id : Int) {
    this(id, "", "")    
  }
  
  def this(id : Int, fn : String)  {
    this(id, fn, "")    
  }

  def print() = {
    println(id + " " + firstName + " " + lastName)
  }
}

The Employee class can be created using multiple constructors defined.

object Employee1Test {
  def main(args:Array[String]) {
    val emp1 = new Employee1(5)
    emp1.print
    val emp2 = new Employee1(5, "John")
    emp2.print
    val emp3 = new Employee1(5, "John", "Smith")
    emp3.print    
  }
}

This example produces the following output.
5  
5 John 
5 John Smith

Proceed to read other Scala Tutorials.

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top