Friday, 15 July 2016

Scala Case Class and Case Object In-Depth

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.


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.
scala> case class Employee(name:String)
defined class Employee

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:-
scala> case object Employee
defined object Employee

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/Deserizable.
  • 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.
  • By default, Case Classes are Immutable.

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.
  • We cannot create an apply method with same number of objects and in same order in our own Companion object. Scala does NOT support it.
  • 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.
    public class Person implements scala.Product ... {
     // Some methods are inherited from Product trait
    }

Scala Case Classes in Inheritance

In this section, We will discuss Case Classes in Inheritance scenarios one by one with some examples.
  • Case Class can NOT extend another Case class.
  • Case Class can extend another Class or Abstract Class.
  • A Case Class or Case Object can extend a Trait.

Scala Case Class Arity

In Scala, Case Classes have Arity. It means how many parameters it accepts.
    case class A(a1:String,a2:String,a3:String,a4:String,a5:String,a6:String,a7:String,a8:String,a9:String,a10:String,a11:String,a12:String,
    a13:String,a14:String,a15:String,a16:String,a17:String,a18:String,a19:String,a20:String,a21:String,a22:String,a23:String,a24:String,
    a25:String,a26:String,a27:String,a28:String,a29:String,a30:String)
    The Arity of A class defined above is 30.
scala> case class Employee(name:String)
defined class Employee
The Arity of A class defined above is 1.

That’s it all about Scala Case Class and Case Object. We will discuss some more Scala concepts in my coming posts.
If you want to learn some more concepts about Scala Case Class and Case Object, please go through my previous posts at:

You can access these tutorials by using the following direct links too:
http://www.journaldev.com/9733/scala-caseclass-caseobject-part1
http://www.journaldev.com/12122/scala-caseclass-caseobject-part2
Please drop me a comment if you like my post or have any issues/suggestions.
Thank you.