Case Class
In this blog post, you’ll learn about -
- What benefits do case classes provide and when you should use them.
Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments.
When you define a case class, the following things happen automatically -
-
Each of the constructor parameters are declared as a val and you’ve getters and setters for each of them.
-
An apply method is provided - this would let you create new objects using the new keyword.
-
Methods toString, equals, copy and hashCode are generated - this would help you serialization, deserialization, comparison and copying objects.
-
Case classes are also optimized for use in Pattern Matching.
All these benefits are seen in the sample code below -
Kaushik Rangadurai
Code. Learn. Explore