Scala Case Class and Case Object In-Depth (Part-1) With Examples

Post Brief TOC

  • Introduction
  • What is Case Class
  • What is Case Object
  • Scala’s Case Class Benefit-1
  • Scala’s Case Class Benefit-2
  • Scala’s Case Class Benefit-3
  • Scala’s Case Class Benefit-4
  • Scala’s Case Class Benefit-5
  • Scala’s Case Class Benefits In Brief
  • Drawback of Scala’s Case Class/Object

Introduction

In this post, we are going to discuss about one of the important concept of Scala Language: Case Class and Case Object.

The main advantage or aim of Case Class concept is that to ease the development by avoiding lot of boilerplate code. We can use Case Classes in Pattern Matching very easily.

As this concept is very important, I’m going to deliver this into two different posts:

We will discuss “Case Class and Case Object Basics” in detail in this post with some suitable examples. Will deliver one more post for some advanced concepts.

What is Case Class?

Case class is also a class, which is defined with “case” modifier. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.

Example:-

Here we have defined a case class with name “Employee” and with one parameter “name”.

What is Case Object?

Case object is also an object which is defined with “case” modifier. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.

Example:-

Here we have defined a case object with name “Employee”.

Let us explore the benefits of Case Classes in-detail with examples.

Scala’s Case Class Benefit-1

First and foremost benefit of Case Class is that Scala Compiler adds a factory method with the name of the class with same number of parameters defined in the class definition.

Because of this benefit, we can create objects of the Case Class without using “new” keyword.

Example:-

If we observe above code snippet, we can create Case class objects without using “new” keyword. It is also allowed “new” keyword, but not recommended to use it here.

Scala’s Case Class Benefit-2

By default, Scala compiler prefixes “val” for all constructor parameters. That’s why without using val or var, Case class’s constructor parameters will become class members, it is not possible for normal classes.

Example:-

By observing above example, we can say that Case class constructor parameters are by default val. We cannot reassign a new value to them once that class object is created. We can access their values by using getter methods, but they don’t have setter methods.

NOTE:- Before discussing the next benefit, we need to understand one point about Case classes.
When we compile a Case class using scalac, what will happen? How many “*.class” files are generated for one Case classes? What are they?

Let us explore this with a simple example:

    • Create a Case class in a source file as shown below

Person.scala

Here we can observe that only one source file “Persona.scala” is available.

  • Open a CMD prompt and compile this source file with scalac as shown below
  • caseclass2-450x249

Here we can observe that Scala Compiler has generated two class files: “Person.class” and “Person$.class”

NOTE:- We will get similar kind of two classes even for Case Objects.

Scala’s Case Class Benefit-3

It is most important feature and very useful. Scala compiler automatically adds “Default Implementation” to toString, hashCode and equals and copy methods.

We can observe this in the following diagram.

caseclass2-450x249

Scala’s Case Class Benefit-4

Scala compiler also adds a copy method to Case class automatically. It is used to create a copy of same instance with modifying few attributes or without modifying it.

We can observe this feature in the above diagram.

Example-1:- To create a new copy of same object without changing the object attributes.

Here, we have created new object s2 by using copy method on s1 object without changing s1 object attributes. That means both are equal as shown below:

Example:- To create a new copy of same object with changing the object attributes.

NOTE:- Here we are using Scala’s Named Parameter Feature. Please read my Scala’s Named Parameter tutorial to understand it well.

Here, we have created new object s3 by using copy method on s1 object without changing s1 object attributes. That means both are NOT equal as shown below:

Scala’s Case Class Benefit-5

Scala compiler also creates a companion object to that Case class and adds apply and unapply methods. We can observe this in the following diagram.

caseclass4-450x115

Example:-
If we create a Case Class as shown below:

Scala Compiler adds a Companion Object with apply and unapply methods as shown below:

So far, we have discussed what are the benefits of Scala’s Case Class. I’m going to list out all those benefits one by one in brief in next section.

NOTE:- Java does NOT have Case class or Case object concept.

Scala’s Case Class Benefits In Brief

Case class is also a class, however when we compare it with normal class, it gives us some extra features or benefits.

The following are the complete list of Advantages/Benefits of Scala’s Case class:

  • By default, Scala Compiler adds toString, hashCode and equals methods. We can avoid writing this boilerplate code.
  • By default, Scala Compiler adds companion object with apply and unapply methods that’s why we don’t need new keyword to create instances of a case class.
  • By default, Scala Compiler adds copy method too.
  • We can use case classes in Pattern Matching.
  • By default, Case class and Case Objects are Serializable.
  • By using Case Classes, we can define Algebraic data types (ADT).
  • They improve productivity that means it avoids writing lot of boilerplate code so that Developers can deliver a functionality with less effort.

All these features are added by Scala Compiler at compile-time. It is not possible with normal class.

Drawback of Scala’s Case Class/Object

As we discussed in the above section, Scala’s Case Class/Object have many benefits. They ease the Development process and improve the the Productivity. However they have only few drawbacks and they are ignorable:

    • As Scala compiler adds lots of boilerplate code for us, compiled class file size is bit more and may have more byte code. If we don’t want to use those default method implementations for equals, toString etc, in that case only we can say it is not useful.
    • Scala Compiler adds some additional functionality by extending scala.Product trait. Most of the time, it is not useful.

If we see compiled class using any Java/Scala Decompiler, we can observe the following code snippet.

That’s it all about Scala Case Class and Case Object Basics. We will discuss Scala Case Class and Case Object Advanced concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions.

By admin

Leave a Reply