Case Class, Traits and Objects

Case Class, Traits and Objects

Recently I started learning Scala first few concepts that learnt while modelling a domain in Scala are Case classes, traits and objects. In this blog I will try to summarize my learnings!

Case Class

Scala being a functional + object oriented language, immutability plays very important role. Case classes are the ways to create immutable data.

case class Shoe(size: Int)
val kidShoe = Shoe(5)

Traits

Traits are similar to Java 8 interfaces, it can have abstract and non abstract methods.

trait ShoeType
       def type: Unit

Object

An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val.

object WearAShoe{
   val adultShoe = Shoe(10)
  def main(arg: Array[String]): Unit = println("I wear a shoe")
}