×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

What is the difference between Scala's case class and class?

By IncludeHelp Last updated : November 07, 2024

Class

Class in Scala is a container for variables and related methods.

Syntax

Syntax for creating a class in Scala:

    class class_name{
        val fields; 
        def methods(parameters); 
    }

Case Class

Case Class in Scala is just like a common class in Scala used in modeling immutable class.

Syntax

Syntax for creating case class in Scala:

case class class_name (parameters)

Points of difference between Case Class and Class in Scala

  1. Both have different syntax for defining. The case class is defined in a single statement with parameters (syntax for defining case class) whereas the normal class is defined by defining method and fields (syntax for defining class).
  2. While creating objects of case class, new keyword is not used which is used to create instances of case class.
  3. Regular classes do not support pattern matching whereas case class support pattern matching.
  4. Regular classes can be inherited by another class whereas case classes cannot be inherited or extended.
  5. Regular classes do not have any predefined method whereas case class has predefined hashcode and equals method.
  6. The comparison of objects of class is done using reference comparison whereas the objects of case class compare the structure of objects.

Let's see these points of difference in more detail to make them clearer,

The difference in Syntax used for defining

Syntax for defining Regular Class:

class class_name{
    val field_name1; 
    val field_name2; 
    ...
    def method1(parameters)
    def method2(parameters)
}

Syntax for defining case Class:

case class case_class_name(parameter)

The difference in syntax for creating objects

In case class, the apply method takes care of object creation whereas objects of regular class are created using the new method.

Syntax for creating object of regular class:

val object_Name = new class_name()

Syntax for creating case object in Scala:

val case_object_name = case_class(parameter values)

Comparison of objects of regular class and case class

The comparison of objects of a regular class is also different from that of case object.

The objects of case class are copied using the swallow copy (learn difference between deep copy and shallow copy in Scala) and the comparison is made by comparing the structure of the object whereas in the case of regular objects the comparison is made as reference (memory).

Program to illustrate the working of Regular Class

// Program to illustrate the working of regular class in Scala 

// Creating a regular class...
class Student(val rlno: Int, val sname: String, val percent: Int) {
    var rollno: Int = rlno
    var name : String = sname
    var percentage = percent
    
    def printresult(){
        print("Roll number : " + rollno)
        print("\nName  : "+ name)
        print("\nHas scored  "+ percentage +" % and is ")
    
        if(percentage > 40)
            println("passed")
        else 
            println("failed")
    }
}

object myObject {
    def main(args: Array[String]) {
        // Creating objects 
        var student1 = new Student(10, "Ramesh", 56)
        println("Object -> Student1 : ")
        student1.printresult()
        var student2 = new Student(10, "Raemsh", 56)
        println("Object -> Student2 : ")
        student2.printresult()
        // Comparing Objects...
        println("student1 == student2 " + (student1 == student2 ))
    }
}

Output:

Object -> Student1 : 
Roll number : 10
Name  : Ramesh
Has scored  56 % and is passed
Object -> Student2 : 
Roll number : 10
Name  : Raemsh
Has scored  56 % and is passed
student1 == student2 false

Program to illustrate the working of case Object in Scala

// Program to illustrate the working of case class in Scala 

// Creating a case class...
case class Student(rollNo: Int, name : String, marks: Int)

object myObject {
    def main(args: Array[String]) {
        // Creating objects 
        var student1 = Student(10, "Ramesh", 56)
        println("Object -> Student1 : " + student1)
    
        var student2 = student1
        println("Object -> Student2 : " + student2)
    
        // Comparing Objects...
        println("student1 == student2 " + (student1 == student2 ))
    }
}

Output:

Object -> Student1 : Student(10,Ramesh,56)
Object -> Student2 : Student(10,Ramesh,56)
student1 == student2 true

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.