Scala Case Class

We learned aboutclassin a previous SKB, today we are going to learn aboutcase class. They serve the same purpose which is to provide a blueprint to createobjects. But case class provides a lot of build-in advantages.

As a start, notice how the case class is being instantiated, and how the fields are being accessed.

Did you notice that we do not neednewto create the instance of theclass?

And did you notice that we do not need a function to access any of the fields?

In Scala,case classprovide build-in things, let's go over them:

  • Constructor: To instantiate an object from a case class, no need for thenewkeyword. Note thatclasswill not needneweither in Scala 3. To understand how this is done, remember the wordsapplyandCompanion Object, we are going to come back to it later.
  • Field accessors: incase class, the field arepublic( we are going to talk aboutvisibilitylater ) by default. So you can access their values without the need to a method that will return it for you.
  • And more; remember the termsunapply,ProductandSerializationfor later SKBs.

Reveal more information and clues
Load Exercise