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 {

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!

Load comments ↻

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