Case classes in Scala are regular classes with some extra toppings that are immutable by default. Let’s discuss why they are in high demand.
A case class with no arguments is declared as a case object rather than a case class. By default, the case object can be serialized. Case class in Scala is constructive in pattern matching.
After that, by default, case classes have public and immutable parameters. In addition, These support pattern matching, making it easier to write logical code.
The Case class in Scala has a default apply() method, which manages the construction of objects.
Before moving toward the examples, let’s check out some characteristics of the case class in Scala.
Characteristics of Case Class in Scala
Scala produces methods like equals(), hashcode(), and toString() automatically as part of the case class.
An instance can be created without using the ‘new‘ keyword.
Scala produces methods like equals(), hashcode(), and toString() automatically as part of the case class.
Benefits of Case Class in Scala
Easy to use in Pattern Matching.
It is immutable.
Case class constructor parameters are val fields by default.
A case class generates a copy() method that is helpful when you want to clone an object or want to update one or more fields during the cloning process.
Case Object in Scala
A case object in Scala is just like an object which has more attributes than a regular object. It is a blend of both case classes and objects.
Characteristics of Case Object in Scala
It has a by default hashCode implementation.
It is serializable
Benefits of Case Object in Scala
Case objects are used when the enumeration is to be done.
Other benefits of case objects is when we want to model the concept of the messages.
This section will cover some examples of case class in Scala.
Syntax:
case class className(parameters)
Defining of Case Class
We have to define a minimal case class; for this, we need the keywords as a case class, an identifier, as well as a parameter list.
// Class case and class object example
case class Color(name: String)
object Main
{
def main(args: Array[String])
{
val x = Color("Orange")
val y = Color("Red");
println(x.name);
println(y.name);
}
}
Output:
Explanation:
Have you noticed that the keyword new was not used to instantiate the Color case class? The reason behind this is that case classes have an apply() method by default which takes care of object construction.
Comparing Two Case Classes
Let’s take a look at how to compare two case classes. Here instances of case classes are compared by structure.
// Comparison of two case class
case class User(Name: String, age: Int)
object Main
{
def main(args: Array[String])
{
val c1 = User("Sagar", 19)
val c2 = User("Sagar", 19)
val c4 = User("Neha", 19)
val c3 = c1 == c2
val c5 = c1 == c4
println(c3);
println(c5);
}
}
Output:
Explanation:
As we see in the above example, variables c1 and c2 are equal, but variables c1 and c4 are not; therefore, for variable c3 true is printed, and for c5 false has been printed.
Shallow Copying a Scala Case Class
Since a Scala case class is immutable, we need a copy to make changes without changing the original. So, we now see how to create a shallow copy of it.
Example:
// Demonstrating the copy() method
object CaseClass extends App {
case class Color(name: String)
val c1 = Color("Yellow")
val c2 = c1.copy()
println(c2.name)
println(c1 == c2)
}
Output:
Explanation:
As we see in the above example, you can create a copy(shallow) of an instance of a case class simply by using the copy() method.
toString Method in Case Class
Let’s see an example of the toString method in the case class in Scala.
// Use of toString() method in case class
case class Bags(color: String, count: Int)
object Main
{
def main(args: Array[String])
{
val x = Bags("Black", 159)
val y = x.count.toString
println("length of converted count is : " + y.length);
}
}
Output:
Explanation:
In the above example, the toString() method returns the int value in string format. So the count of bags was 159, and then after applying the toString method, it converted to 3.
Duplicate Same Object with Changing Attributes
Let’s see how to duplicate the same case object with changing attributes.
// Example of a duplicate of the same object with changing attributes.
case class User(name: String, age: Int)
object Main
{
def main(args: Array[String])
{
val c1 = User("Neha", 27)
// Display parameter
println("Name is: " + c1.name);
println("Age is: " + c1.age);
val c2 = c1.copy(age = 29)
// Display copied and changed attributes
println("Copy Name is: " + c2.name);
println("Change Age is: " + c2.age);
}
}
Output:
Explanation:
Here you need to pass the new value in the copy method.
Scala is an object-oriented, high-level programming language that supports the functional programming approach.
What do you mean by Case Class in Scala?
Case classes in Scala are regular classes with some extra toppings that are immutable by default.
What do you mean by a Shallow copy?
A shallow copy is one where changes to the copy reflect the original.
What do you mean by a Case Object in Scala?
A case object is just like an object but has more attributes than a regular object. A case object has more features than a regular object.
How can we define a minimal case class in Scala?
To define a minimal Scala Case Class, we need the keywords ‘case class’, an identifier, and a parameter list. We can keep the parameter list empty.
Conclusion
Thanks, Ninjas, for making it to the end of the blog. Hope you enjoyed it and have gained some knowledge about case class in Scala, case objects in Scala, Comparison of two case classes, Shallow copying of a scale case class, the toString method in case class, and at last, how to create a duplicate of the same object with changing the attributes with the help of the examples.