Thursday, 10 September 2020

Scala: Classes

 Syntax

class ClassName(instanceVariables) {

    ......

    ......

}

 

Example

class Employee(id: Int, firstName: String, lastName: String) {

  def getId = {
    id
  }

  def getFirstName = {
    firstName
  }

  def getLastName = {
    lastName
  }

  override def toString = {
    s"id = $id, firstName: $firstName, lastName: $lastName"
  }
}

Above snippet define an Employee class with three instance properties.

a.   id of type Int

b.   firstName of type String

c.    lastName of type String

 

Employee class defines four instance methods.

a.   getId

b.   getFirstName

c.    getLastName

d.   toString : This method overrides super class definition of toString method. ‘override’ keyword is used to specify that we are overriding super class definition of this method.

 

Above snippet creates a default constructor with all the instance properties.

 

How to define an object?

Using new keyword, you can define an object.

 

Syntax

var/val variableName = new ClassName(instanceProperties)

 

Example

val emp1 = new Employee(1, "Krishna", "Gurram")

 

How to access instance methods?

Using ‘.’ Operator, you can access instance methods.

 

Example

emp1.getId

 

Since getId() method does not take any arguments, you can omit paranthesis ().

scala>  class Employee(id: Int, firstName: String, lastName: String){
     |  
     |      def getId = {
     |          id
     |      }
     |      
     |      def getFirstName = {
     |          firstName
     |      }
     |      
     |      def getLastName = {
     |          lastName
     |      }
     |      
     |      override def toString = {
     |          s"id = $id, firstName: $firstName, lastName: $lastName"
     |      }
     |  }
class Employee

scala> 

scala> val emp1 = new Employee(1, "Krishna", "Gurram")
val emp1: Employee = id = 1, firstName: Krishna, lastName: Gurram

scala> emp1.getId
val res227: Int = 1

scala> emp1.toString
val res228: String = id = 1, firstName: Krishna, lastName: Gurram

Instance properties are immutable by default

Instance properties are immutable by default, to confirm lets add setter methods and execute.

class Employee(id: Int, firstName: String, lastName: String) {

  def getId = {
    id
  }

  def getFirstName = {
    firstName
  }

  def getLastName = {
    lastName
  }

  override def toString = {
    s"id = $id, firstName: $firstName, lastName: $lastName"
  }

  def setId(id: Int) = {
    this.id = id
  }

  def setFirstName(firstName: String) = {
    this.firstName = firstName
  }

  def setLastName(lastName: String) = {
    this.lastName = lastName
  }
}

When you try to define above Employee class, Scala compiler throws below errors.

On line 20: error: reassignment to val
           this.firstName = firstName
                          ^
On line 24: error: reassignment to val
           this.lastName = lastName
                         ^
On line 28: error: reassignment to val

How to resolve this error?

Define instance properties with ‘var’ keyword.

class Employee(var id: Int, var firstName: String, var lastName: String) {
  ......
  ......
}

Example

scala> class Employee(var id: Int, var firstName: String, var lastName: String) {
     | 
     |   def getId = {
     |     id
     |   }
     | 
     |   def getFirstName = {
     |     firstName
     |   }
     | 
     |   def getLastName = {
     |     lastName
     |   }
     | 
     |   override def toString = {
     |     s"id = $id, firstName: $firstName, lastName: $lastName"
     |   }
     | 
     |   def setId(id: Int) = {
     |     this.id = id
     |   }
     | 
     |   def setFirstName(firstName: String) = {
     |     this.firstName = firstName
     |   }
     | 
     |   def setLastName(lastName: String) = {
     |     this.lastName = lastName
     |   }
     | }
class Employee

You can update Employee instance using setter methods.

scala> val emp1 = new Employee(1, "Krishna", "Gurram")
val emp1: Employee = id = 1, firstName: Krishna, lastName: Gurram

scala> emp1.setFirstName("Ram")

scala> emp1
val res231: Employee = id = 1, firstName: Ram, lastName: Gurram

scala> emp1.setId(123456)

scala> emp1
val res233: Employee = id = 123456, firstName: Ram, lastName: Gurram

Note

a.   Wherever possible always use immutable variables.




Previous                                                    Next                                                    Home

No comments:

Post a Comment