Google News
logo
Scala - Interview Questions
What are case classes in Scala?
Scala case classes are like regular classes except for the fact that they are good for modeling immutable data and serve as useful in pattern matching. Case classes include public and immutable parameters by default. These classes support pattern matching, which makes it easier to write logical code.  The following are some of the characteristics of a Scala case class:

* Instances of the class can be created without the new keyword.
* As part of the case classes, Scala automatically generates methods such as equals(), hashcode(), and toString().
* Scala generates accessor methods for all constructor arguments for a case class.

Syntax :
 case class className(parameters)​

Example :
 case class Student(name:String, age:Int)    
object MainObject
{                                                                                             
    def main(args:Array[String])
    {   
        var c = Student(“Visthara”, 23)                                    
        println("Student name:" + c.name);                    
        println("Student age: " + c.age);   
    }   
}   ​

Output :
Student Name: Visthara
Student Age: 23​
Advertisement