Explanation of Case Class
A Case Class is just like a regular class, which has a feature for modeling unchangeable data. It is also constructive in pattern matching. It has been defined with a modifier case, due to this case keyword, we can get some benefits to stop oneself from doing a sections of codes that have to be included in many places with little or no alteration. As we can see below a minimal case class needs the keyword case class, an identifier, and a parameter list which may be vacant.
Syntax:
Case class className(parameters)
Note: The Case class has a default apply() method which manages the construction of object.
Explanation of Case Object
A Case Object is also like an object, which has more attributes than a regular Object. It is a blend of both case classes and object. A case object has some more features than a regular object.
Below two are important features of case object:
- It is serializable.
- It has a by default hashCode implementation.
Example :
case class employee (name : String, age : Int)
object Main
{
def main(args : Array[String])
{
var c = employee( "Nidhi" , 23 )
println( "Name of the employee is " + c.name);
println( "Age of the employee is " + c.age);
}
}
|
Output:
Name of the employee is Nidhi
Age of the employee is 23
Some Benefits of Case Class/Object
- The one of the topmost benefit of Case Class is that Scala Compiler affix a method with the name of the class having identical number of parameters as defined in the class definition, because of that you can create objects of the Case Class even in the absence of the keyword new.
Example:
case class Book (name : String, author : String)
object Main
{
def main(args : Array[String])
{
var Book 1 = Book( "Data Structure and Algorithm" , "cormen" )
var Book 2 = Book( "Computer Networking" , "Tanenbaum" )
println( "Name of the Book1 is " + Book 1 .name);
println( "Author of the Book1 is " + Book 1 .author);
println( "Name of the Book2 is " + Book 2 .name);
println( "Author of the Book2 is " + Book 2 .author);
}
}
|
Output:
Name of the Book1 is Data Structure and Algorithm
Author of the Book1 is cormen
Name of the Book2 is Computer Networking
Author of the Book2 is Tanenbaum
- The second convenience is that by default Scala compiler affixes val or var for all the parameters of constructor so, we won’t be able to reassign a new value to them once that class object is constructed that’s why even in absence of val or var, case class’s constructor parameters will turn out to be class members, that is not practicable for regular classes.
Example:

Thus, reassignment is not feasible.
- The Scala compiler also appends a copy() method to case class that is utilized to create a duplicate of the same object with changing some parameters or without altering them.
Example : To create a duplicate of same instance without altering the parameters.
case class Student (name : String, age : Int)
object Main
{
def main(args : Array[String])
{
val s 1 = Student( "Nidhi" , 23 )
println( "Name is " + s 1 .name);
println( "Age is " + s 1 .age);
val s 2 = s 1 .copy()
println( "Copy Name " + s 2 .name);
println( "Copy Age " + s 2 .age);
}
}
|
Output:
Name is Nidhi
Age is 23
Copy Name Nidhi
Copy Age 23
Here, we have created new object s2 by using copy method on s1 object without altering s1 object attributes.
Example : To create a duplicate of same object with changing attributes.
case class Student (name : String, age : Int)
object Main
{
def main(args : Array[String])
{
val s 1 = Student( "Nidhi" , 23 )
println( "Name is " + s 1 .name);
println( "Age is " + s 1 .age);
val s 2 = s 1 .copy(age = 24 )
println( "Copy Name is " + s 2 .name);
println( "Change Age is " + s 2 .age);
}
}
|
Output:
Name is Nidhi
Age is 23
Copy Name is Nidhi
Change Age is 24
Here, you need to pass the value in the copy method.
- By default Scala Compiler adds toString, equals methods, companion object with apply and unapply methods, for that reason you don’t need new keyword to construct object of a Case class.
Note:It was not practicable to use more than 22 parameters in scala case classes but now you can use any number of parameters in case classes using Scala Version 2.11.1. This limitation was eliminated from this version of scala.