FUNCTIONALITY OF CASE CLASSES IN SCALA

Reading Time: 2 minutes

Object-oriented programming (Oops) is a programming paradigm based on the concept of “objects”, which is physical entity of a class. Classes are nothing but a blueprint or prototype in object oriented programming. We can also say it is virtual entity. Now a days object oriented programming languages very often provide some hidden functionalities i. e. abstraction to represent simple, immutable data. Scala provides this kind of abstraction through Case Classes. Let’s see functionality of case classes in Scala.

How to define case class:

It’s very easy to define a case class in Scala.

Syntax :

case class scalaCaseClass(Vehicle :  String, Average :  String)

Where case class is a keyword provided by Scala community to define a case class same as we declare classes with class keyword in other programming language like java, python or c++. scalaCaseClass is the class name, It’s user specific you can use any name. Vehicle & Average are the case class variables.

Example :

case class scalaCaseClass(Vehicle:String, Average:String)  
object mainMethodObject{  
    def main(args:Array[String]){  
 // Creating object of case class  
        val scalaCC =  scalaCaseClass(“Car”, “15km/h”)
// Accessing elements of case class   
        println("a = "+scalaCC .Vehicle )               
        println("b = "+scalaCC .Average )  
    }  
} 

Functinality of case class in SCALA:

  • There are no need to put new keyword to create an instance of case class, It is possible because case classes have an apply() function auto-generated by the compiler. It will provide better readability in complex scenario like nested classes.
var scalaCC = scalaCaseClass(“Car”, “15km/h”)
  • Case classes are immutable
    By default, all the variables are val type in Scala if a variable is val type we can’t update it but if want to update the then we need to declare it as var type.
case class scalaCaseClass(var Vehicle:String, var Average:String)
  • We can directly compare objects of case classes with == operator which is not possible in normal object
Example  :
class scalaClass(val Vehicle1: String, val Average1: String)
case class scalaCaseClass(Vehicle: String, Average:String)  
 object Test {
    val sc1 = new scalaClass(“bike”, ”50km/h”)
    val sc2 = new scalaClass(“bike”, ”50km/h”)
    println(sc1 == sc2)   //this returns false

    val scalaCC1=  scalaCaseClass(“Car”, “15km/h”)
    val scalaCC2=  scalaCaseClass(“Car”, “15km/h”)
    println( scalaCC1 == scalaCC2)   //this returns true
 }
  • As we discussed scala case classes are immutable in nature if we want to change something then we need to create copy of that class. To create a copy of that class scala provide a copy method which create copy of the case class
Example:
case class scalaCaseClass(Vehicle:String, Average:String)
val scalaCC =  scalaCaseClass(“Car”, “15km/h”)

//copy of  scalaCaseClass with different vehicle
val  copyofScalaCC =  scalaCC.copy( Vehicle =”Fortuner”)
  • We can also use case classes in pattern matching.
Example: 

trait scalaTrait  
case class scalaCC1(Vehicle:String, Average:String) extends scalaTrait  
case class scalaCC2(Vehicle:String) extends scalaTrait         // Case class  
case object scalaCaseObject extends scalaTrait               // Case object  
object MainObject{  
    def main(args:Array[String]){  
        callScalaCase(scalaCC1(“Car” , “15km/h”))  
        callScalaCase(scalaCC2(“Car”))  
        callScalaCase(scalaCaseObject)  
    }  
    def callScalaCase(f : scalaTrait) = f match{  
        case scalaCC1(f, g) => println("vehicle = " + f + " average = " + g)  
        case scalaCC2(f) => println("vehicle = " + f)  
        case scalaCaseObject => println("This is scala case object") 
        case _ => println("No case match so default case executed") 
    }  
}

REFERENCES

https://docs.scala-lang.org/tour/case-classes.html

knoldus

Written by 

Nikhil Gupta is software consultant in knoldus. I am punctual, detailed oriented, multitasking and innovative. I believe that practice makes a man perfect with full commitment, planned strategies.