Scala | Case Class and Case Object

28 Feb, 2019
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.
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 :
// Scala program of case class and case Objectcase class employee (name:String, age:Int)object Main {     // Main method    def main(args: Array[String])     {         var c = employee("Nidhi", 23)                  // Display both Parameter         println("Name of the employee is " + c.name);         println("Age of the employee is " + c.age);     } } 
Name of the employee is Nidhi
Age of the employee is 23

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:// Scala program of case class and case Object// affix a method with the name of the classcase class Book (name:String, author:String)object Main{    // Main method    def main(args: Array[String])    {        var Book1 = Book("Data Structure and Algorithm", "cormen")        var Book2 = Book("Computer Networking", "Tanenbaum")                  // Display strings        println("Name of the Book1 is " + Book1.name);        println("Author of the Book1 is " + Book1.author);        println("Name of the Book2 is " + Book2.name);        println("Author of the Book2 is " + Book2.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

// Scala program of case class and case Object// affix a method with the name of the classcase class Book (name:String, author:String)object Main{    // Main method    def main(args: Array[String])    {        var Book1 = Book("Data Structure and Algorithm", "cormen")        var Book2 = Book("Computer Networking", "Tanenbaum")                  // Display strings        println("Name of the Book1 is " + Book1.name);        println("Author of the Book1 is " + Book1.author);        println("Name of the Book2 is " + Book2.name);        println("Author of the Book2 is " + Book2.author);    }} 
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.// Scala program of case class To create // a duplicate of same instancecase class Student (name:String, age:Int)object Main{    // Main method    def main(args: Array[String])    {        val s1 = Student("Nidhi", 23)                  // Display parameter        println("Name is " + s1.name);        println("Age is " + s1.age);        val s2 = s1.copy()                  // Display copied data        println("Copy Name " + s2.name);        println("Copy Age " + s2.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.// Scala program of case class same object // with changing attributescase class Student (name:String, age:Int)  object Main{    // Main method    def main(args: Array[String])    {        val s1 = Student("Nidhi", 23)                  // Display parameter        println("Name is " + s1.name);        println("Age is " + s1.age);        val s2 = s1.copy(age = 24)                  // Display copied and changed attributes        println("Copy Name is " + s2.name);        println("Change Age is " + s2.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.
// Scala program of case class To create // a duplicate of same instancecase class Student (name:String, age:Int)object Main{    // Main method    def main(args: Array[String])    {        val s1 = Student("Nidhi", 23)                  // Display parameter        println("Name is " + s1.name);        println("Age is " + s1.age);        val s2 = s1.copy()                  // Display copied data        println("Copy Name " + s2.name);        println("Copy Age " + s2.age);    }} 
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.
// Scala program of case class same object // with changing attributescase class Student (name:String, age:Int)  object Main{    // Main method    def main(args: Array[String])    {        val s1 = Student("Nidhi", 23)                  // Display parameter        println("Name is " + s1.name);        println("Age is " + s1.age);        val s2 = s1.copy(age = 24)                  // Display copied and changed attributes        println("Copy Name is " + s2.name);        println("Change Age is " + s2.age);    }} 
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.
Picked
Scala
Scala-OOPS
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Scala Map
Scala Lists
Scala | Arrays
Lambda Expression in Scala
How to Install Scala with VSCode?
Enumeration in Scala
How to get the first element of List in Scala
Scala String replace() method with example
HashMap in Scala
Scala List contains() method with example