top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Scala: What are the differences between Case class and Normal Class?

+1 vote
639 views
Scala: What are the differences between Case class and Normal Class?
posted Jun 28, 2016 by Satyam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits:

1.By default, Case-class constructor parameters are ‘val’. We don’t need to declare parameters with ‘val’.
2.By default, Case-class constructor parameters become class fields.
3.These methods are added automatically: toString, equals, hashCode, copy. apply and unapply.
4.It automatically gets Companion object.
5.No need to use ‘new’ keyword to create instance of Case Class.
6.Easy to use in Pattern Matching.
All these features are added by Scala Compiler at compile-time. It is not possible with normal class.

answer Jun 28, 2016 by Shivam Kumar Pandey
...