Home » Scala language

Difference between case object and object in Scala

Scala case object vs object: Scala programming language allows programmers to create a special type of objects that have added features. In this tutorial, we will learn the difference between case object and object in Scala.
Submitted by Shivang Yadav, on August 17, 2019

Scala case object vs object

1) object

An object is an instance of a class, it can also be seen as a class that has only a single instance. Like class, you can create fields and methods for object too. Example,

    object Myclass{
        def add();
    }

2) case object

case object is just like an object but with some added features and attributes. It can be seen as a blend of case class ( the defined for adapting to changes) and object. It is created with a modifier case. Example,

    case object a {
		    //definition. 
    }

Main features of Scala case object are,

  • It is implemented as hash code by default.
  • It is serializable.

The case objects are compatibility with pattern matching in Scala.

Difference between case object and object in Scala

These are the following things that make a case object special,

  • It is implemented as hash-code by default.
  • Serializability of case objects.
  • It has support for pattern matching.
  • By default implementation in tostring.

These special types of object are created to imply some added features to the general object making it usage special.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.