ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Case Class in Scala: How to Create a Case Object?

Here, we will learn about the case class in Scala. A case class is a special type of class that has multiple benefits over regular class. We will learn about the case class, creating case objects, and some operations related to it.
Submitted by Shivang Yadav, on December 20, 2020

Case Class - Definition

The case class is a special type of class in Scala. It is similar to regular class and has some added features that are helpful in modeling immutable data and pattern matching. The case class uses the apply () method to deal with the construction of new objects. The case class in Scala is immutable and so are all of its members, i.e., you cannot change the value of them.

Creating a Case Class

The creation of case class is simple, the keyword case class is used to define it followed by its name and a set of parameters that can be empty.

Syntax:

case class cc_Name (paramter(s))

Code snippet to create a case class in Scala

case class student(name: String, class: Int, marks: Int, result: String)

Case Objects

The case objects are nothing but the instances of case class in Scala.

Creating case objects

obj_name = cc_name(parameter_values)

Could you see and understood, there is no new keyword used in the definition of case object which is because it uses the apply() method.

Program to illustrate the creation of case objects in Scala

// Program to illustrate the creation of case objects in Scala

// Creating a Case Class
case class student(name: String, standard: Int, marks: Int, result: String )

object myObject {
    def main(args: Array[String]) {
        // Creating a case Object 
        val student1 = student("Prem", 10, 89, "A+")
        println("Case Object: " + student1)
    }
}

Output:

Case Object: student(Prem,10,89,A+)

Operations on the case class

Comparing Case Objects

In Scala, case objects can be compared using == expression which returns true or false based on the comparison.

Syntax:

case_Obj1 == case_Obj2

Program to illustrate the comparison of case objects

// Program to illustrate the comparison of case objects in Scala

// Creating a Case Class
case class student(name: String, standard: Int, marks: Int, result: String )

object myObject {
    def main(args: Array[String]) {
        // Creating a case Object 
        val student1 = student("Prem", 10, 89, "A+")
        println("Case Object student1 : " + student1)
    
        val student2 = student("Ramesh", 11, 56, "B")
        println("Case Object student2 : " + student2)
    
        val student3 = student("Prem", 10, 89, "A+")
        println("Case Object student3 : " + student3)
        
        val comp1 = student1 == student2
        println("Comparison of student1 and student2 = " + comp1)
        
        val comp2 = student1 == student3
        println("Comparison of student1 and student3 = " + comp2)
    }
}

Output:

Case Object student1 : student(Prem,10,89,A+)
Case Object student2 : student(Ramesh,11,56,B)
Case Object student3 : student(Prem,10,89,A+)
Comparison of student1 and student2 = false
Comparison of student1 and student3 = true

Coping Case Class in Scala

We can copy the contents of one case object to another fully and partially too.

Copying case object using copy() method [shallow copy]:

The copy() method on the case object is used to copy the contents of one object to another. This is a shallow copy.

Syntax:

caseObj1 = caseObj2.copy()

Program to illustrate object copy using copy() method

// Creating a Case Class
case class student(name: String, standard: Int, marks: Int, result: String )

object myObject {
    def main(args: Array[String]) {
        // Creating a case Object 
        val student1 = student("Prem", 10, 89, "A+")
        println("Case Object student1 : " + student1)
        
        val copyObj = student1.copy()
        println("Copied Object: " + copyObj)
    }
}

Output:

Case Object student1 : student(Prem,10,89,A+)
Copied Object: student(Prem,10,89,A+)

Copying some constructor arguments from an object

The copy() method can also be used to create a copy object and copy only some parameters from the list.

Syntax:

copy_CaseObject = caseObj.copy(parameters)

Example:

// Creating a Case Class
case class student(name: String, standard: Int, marks: Int, result: String )

object myObject {
    def main(args: Array[String]) {
        // Creating a case Object 
        val student1 = student("Prem", 10, 89, "A+")
        println("Case Object student1 : " + student1)
        
        val copyObj = student1.copy(name = "Ria", student1.standard, marks = 85, student1.result)
        println("Copied Object: " + copyObj)
    }
}

Output:

Case Object student1 : student(Prem,10,89,A+)
Copied Object: student(Ria,10,85,A+)
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.