Connecting...

Pexels Photo 92905

Case class and object in Scala by Blog Knoldus

Pexels Photo 92905

Have a look at one of the more important concepts of Scala, case class!

Also, delve deeper into companion object, what is this? And how is it created in Scala? Thanks to Blog Knoldus we will find out.

 

'In this blog, we are going to have a look at one of the important concept of Scala that is case class moreover we will also take a look at companion object. Before diving into a case class let’s first look at scala basics. 

Construct like class, traits, object, the package can have characteristics and behaviour. Scala has access modifier to restrict the use of this by default it is public. Scala provides accessibility using a public, private, protected access modifier

A public member can be accessed outside the class and by default all member of the class is public A private member is visible only inside the class or object that contains the member definition A protected member can be accessed in a subclass of the class So this is the access modifier which is provided in scala example below show how to use it

class Bank{
 val ifsc:String = "SBI007"
 protected val name:String = "SBI"
 private val customerCount:Long = 123456789 } 
class BankDetail extends Bank{
 print(customerCount)  // cannot be accessed
 print(ifsc)
 print(name) }  

So now we know the basic how the methods and variable are accessed in Scala future going to case class let take a look at companion object.

 

What is a Companion object?

Scala classes cannot have static variables or methods. Instead of a Scala class can have what is called a singleton object or sometimes a companion object if a class and object share the same name and are in the same source file are said to be companion object where companion class can access all the field and methods from companion objects including private given. Below example demonstrate how companion object is created in Scala.

class CompanionClass(message:String = CompanionClass.message){
 println(message)}  
object CompanionClass{
 private val message:String = "hello"
 def fun() = {
   print("HI")}}

Here comes the case class. The first question which may come in your mind is how case class are different from normal class? The answer to all your question is here.

 

Why Scala have Case Class?

Case class is scale way to allow pattern matching on an object without requiring a large amount of boilerplate. All you need to do is add a single case keyword modifier to each class that you want to pattern matching using such modifier makes scala compiler add some syntactic conveniences to your class and compiler add companion object(with the apply method)
Adds factory method with the name of the class this means that for instance, you can write StringValue(“X”) to construct a StringValue object instead of using new StringValue(“X”)

case class StringValue(name:String)
val stringValue = StringValue("X")

It makes every class parameter field which is immutable by default

case class Time(hour:Int = 0 , minute:Int = 0)
val time = Time(12)
time.hour

Perform value based equivalence by default and compiler add implementations of toString hashCode equals to your class The constructor parameters you specify become properties with getter and setters. 
This is how case class are different from the normal class in Scala Along with case, class comes case object.

 

What is Case Object?

A case object, on the other hand, does not take arguments in the constructor, so there can only be one instance of it(a singleton like a regular scale object. A case object, on the other hand, does not take args in the constructor, so there can only be one instance of it (a singleton like a regular scale object is). A case object is a singleton case class. If you create a case class with no parameter it is stateless and should be case object.

case class Time   // won’t compile
case object Time  // one instance in JVM
Time.toString
case class Time   // won’t compile
case object Time  // one instance in JVM
Time.toString

There are no copy methods since this is a singleton.
No method for structural equality comparison.
Case object is serializable by default.

 

References

1.https://www.scala-lang.org/
2.Programming in Scala by Martin Odersky, Lex Spoon, Bill Benners '

 

This article was written by and originally posted on Blog Knoldus.