Scala Case Class and Case Object In-Depth (Part-2) With Examples

Post Brief TOC

  • Introduction
  • Scala’s Case Class Benefit-6
  • Advantages of Scala’s Case Classes
  • Case Class Parameters
  • Case Class Parameters with “var”
  • Limitation of Case Class Parameters
  • Case Object Vs Object(Normal Object)
  • Case Class/Object is Serializable
  • Case Classes may be abstract
  • Case Class/Object usage in Real-time Projects
  • Case Classes in Inheritance

Introduction

I have already discussed about Scala Case Class and Case Object basics in my previous post. Before going through this post, please read my previous post at: “Scala Case Class and Case Object Basics”

In this post, we are going to discuss some Advanced Concepts and Real-time Projects usage about Scala Case Class and Case Object. Please practice each example without fail to get some confidence on this concept.

Scala’s Case Class Benefit-6

As we discussed in my previous post at “Scala Case Class and Case Object Basics” in “Scala’s Case Class Benefit-2” section, Case Class parameters are by-default “val” that means they are constants.

Because of this feature, by nature Scala’s Case Classes are Immutable. Once we create an instance of a Case Class, we cannot update it’s parameters.

NOTE:- If we want to develop Immutable classes in Java, we need to follow some set of rules. However in Scala, it is easy to develop Immutable classes.

Advantages of Scala’s Case Classes

Scala’s Case Classes have the following advantages:

  • By default, they are Immutable.
  • They avoid lot of boilerplate code.
  • They ease the development process and improve Productivity.
  • They are very easy to use in Pattern Matching.

Case Class Parameters

In Scala’s Older versions, We can create Case Classes without Parameters. However, it’s NOT possible in recent Scala Releases. If we try to create a Case Class without Parameters or Fields, we will get compile-time error as shown below:

This has been deprecated since Scala version 2.7.7. It is removed in 2.11.1.

So Scala Version 2.11.1 on-wards, we cannot create Case Classes without parameters.

If we don’t have any parameters, then We can create Case Class using empty parenthesis as shown blow:

Or it’s always advisable to use Case Object in this case.

Case Class Parameters with “var”

As we know, by default Case Class Parameters are val. Even though, it is not recommended to change this nature, but we can change Case Class Parameters from val to var as shown below:

As we know, “var” means variables. We can change it’s values.

By Default, Scala Compiler generates only getters for Case Class Parameters. However, if we use “var” to Case Class Parameters, Scala Compiler generates both setters and getters.

Limitation of Case Class Parameters

Before Scala Version 2.11.1, Scala Case Classes support maximum of 22 parameters only. If we use more than 22, we will get compile-time error.

However, this limitation has been removed in Scala Version 2.11.1. Now, Scala Case Classes supports more than 22 Parameters.

That means we can use any number of Parameters in Case Classes.

Case Object Vs Object(Normal Object)

Normal object is created using “object” keyword. By default, it’s a singleton object.

Case Object is created using “case object” keywords. By default, it’s also a singleton object

By Default, Case Object gets toString and hashCode methods. But normal object cannot.
By Default, Case Object is Serializable. But normal object is not.

Case Class/Object is Serializable

Yes, By Default, Case Object is Serializable. But normal object is not. We can prove this by using isInstanaceOf method as shown below:

We can prove it in another way. If we decompile the class file, we can observe the following code.

Case Classes may be abstract

Before Scala Version 2.7.0, we cannot create Case Classes as Abstract. However Scala Version 2.7.0 on-wards, Case Classes may be Abstract. We can create them as Abstract using “abstract” modifier as shown below:

We should use “abstract” modifier before “case” modifier only like “abstract case class”. If we use it in other way, we will get compile-time error as shown below:

Case Class/Object usage in Real-time Projects

If you are new to Scala Development, you should know this point to get Scala Jobs and easy to start Scala Development Projects.

In Real-time Projects, we use Case Classes to define application MODEL objects (Here model means M in MVC. In other words, Model means POJO, Java Bean etc).

Case Classes in Inheritance

In this section, We will discuss Case Classes in Inheritance scenarios one by one with some examples.

    • Case Class can NOT extend another Case class.

Before Scala Version 2.7.0, a Case Class cannot extend from other Case Class. However, They allowed to create a Case class by extending another case class in Scala Version 2.7.0 as shown below:

However, they have removed this feature in recent Scala Versions. So a Case Class cannot extend another Case class.

NOTE:- In Scala Language, case-to-case inheritance is prohibited.

  • Case Class can extend another Class or Abstract Class.

  • A Case Class or Case Object can extend a Trait.

I hope, this much information is enough to work on Scala-based projects. However, there are many more concepts to discuss about Case Classes. If anybody interested to know more, please drop me a comment below.

NOTE:- We will discuss about “How to use Case Class/Object in Pattern Matching” to understand the main benefit of Case Class/Object concept in my coming post “Scala Pattern Matching In-Depth (Coming soon).

That’s it all about Scala Case Class and Case Object. We will discuss some more Scala concepts in my coming posts.

Please drop me a comment if you like my post or have any typo errors/issues/suggestions.

By admin

Leave a Reply

%d bloggers like this: