In Previous chapter we learned about SCALA SINGLETON AND COMPANION OBJECT and today lets check out SCALA CASE CLASS.

A Case Class is just like a regular class with additional functionality built into it. It has all the benefits of a regular class plus more.

SCALA CASE CLASS SYNTAX

The syntax of case class is pretty simple, its like a class with keyword case added at the beginning.

case class <class_name> (<var_nm_1>:TYPE ,<var_nm_n>:TYPE )

Th Inbuilt functionalities are explained as topics below

SCALA CASE CLASS DEFAULT CONSTRUCTOR PARAMETER

Case Class parameters are by default PUBLIC VAL. Which means that the parameters which behave as fields have only getter method and no setter method. Also the fields can be accessed outside the class.

scala> case class student(name:String , age:Int)
defined class student

scala> val p1 = student("Smith",24)
p1: student = student(Smith,24)

scala> println(p1.name)
Smith

scala> p1.name = "Jaden"
<console>:25: error: reassignment to val
       p1.name = "Jaden"

In the above example you see that because the parameter is val i can get the values but cannot set any value. Also notice that when i create an instance p1 i did not have to use keyword ‘new’, this is because Scala Case Class has an apply method by default. Lets understand this more in our next topic.

SCALA CASE CLASS APPLY METHOD

When you create a case class , Scala creates a companion object by default with an APPLY METHOD having the same parameters as the case class. So as discussed before you can create an instance of case class without using the ‘new’ keyword.

Lets check this with an example

case class student (name:String , age:Int)

//A companion object like below is created automatically
object student {
def apply(name:String , age:Int) = new student (name , age)
}

Remember, you can create your own apply method with the same number of parameters as the case class and Scala would override your apply method with default one.

CASE CLASS COPY METHOD

Case Class has a built in copy method which can 1)Copy an Object 2)Modify one or more fields during the copy process.
Lets say we have a student class and we need to create 2 student objects for the same.

scala> case class student(name:String , age:Int , country :String)
defined class student

scala> val objSmith = student("Smith",23,"Canada")
objSmith: student = student(Smith,23,Canada)

Now I need to add another student Mike who is same age as Smith and comes from same country Canada. In such case instead of providing all parameters I can simple copy objSmith which has most of the information and only change the name parameter.

scala> val objMike = objSmith.copy(name = "Mike")
objMike: student = student(Mike,23,Canada)

EQUALS AND HASHCODE METHODS

Case classes also have equals and hascode methods to compare instances. There are different ways by which we can compare let check them.

scala> case class student(name:String)
defined class student

scala> val obj1 = student("Smith")
obj1: student = student(Smith)

scala> val obj2 = student("Risabh")
obj2: student = student(Risabh)

scala> val obj3 = student("Smith")
obj3: student = student(Smith)

//Using ==
scala> obj1 == obj2
res4: Boolean = false

scala> obj1 == obj3
res5: Boolean = true

//Using  .equals
scala> obj1.equals(obj3)
res6: Boolean = true

Now hashCode() creates a hash number for object.

scala> obj1.hashCode()
res9: Int = -2123356034

ToString METHOD

When you create an instance of a class and then type in the instance and hit enter, you will see that Scala prints the class name followed by some code. But when you do the same with Case Class you see that it shows the value held in that instance. This is possible because of inbuilt ToString() method. Lets check this with an example.

scala> class teacher(name:String)
defined class teacher

scala> val obj1 = new teacher("Daisy")
obj1: teacher = teacher@1b641c97

scala> obj1
res5: teacher = teacher@1b641c97

scala> case class teacher(name:String)
defined class teacher

scala> val obj2 = teacher("Mickey")
obj2: teacher = teacher(Mickey)

scala> obj2
res6: teacher = teacher(Mickey)

UNAPPLY METHOD

Simply put, an apply method takes in arguments and creates an instance of the class, whereas an unapply method takes in an instance and returns the arguments. They are famously used for pattern matching.

Lets create an empty trait school

scala> trait school {}
defined trait school

Create instances of student and teachers. A method which takes in arguments of types student. These pattern case Student( , ) => work because of unapply method.

scala> case class Student(name: String, year: Int) extends school
defined class Student

scala> case class Teacher(name: String, specialty: String) extends school
defined class Teacher

scala> def exp(s: school): String = s match {
     |     case Student(name, year) =>
     |         s"$name is a student in Year $year"
     |     case Teacher(name, subject) =>
     |         s"$name teaches $subject subject"
     | }
exp: (s: school)String

scala> val a = Student("Smith", 2021)
a: Student = Student(Smith,2021)

scala> val b = Teacher("Hentre Mat", "History")
b: Teacher = Teacher(Hentre Mat,History)

scala> exp(a)
res1: String = Smith is a student in Year 2021

scala> exp(b)
res2: String = Hentre Mat teaches History subject

You can learn more about SCALA PATTERN MATCHING here.

In the next chapter we will learn about SCALA FUNCTIONS

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from UnderstandingBigData

Subscribe now to keep reading and get access to the full archive.

Continue reading