Scala Tutorial - Learn How To Extend Case Class - Case Class Inheritance

By Nadim Bahadoor | Last updated: March 16, 2018 at 13:18 pm

Overview

In this tutorial, we will continue from what we've learned from the previous tutorial on Class Inheritance in Scala.  The examples in this tutorial will be focused on case classes which will extend an abstract class - case class inheritance.

 

If you recall from the tutorial on What is Scala programming language, Scala is both an Object Oriented and Functional programming language. As a result of its Object Oriented nature, it has full support of object hierarchies through the use of class inheritance.



 

Steps

1. How to define an abstract class called Donut 

When dealing with class and object hierarchies, you typically have a base class which encapsulates common behaviour. As an example, let's create an abstract class name Donut which defines a method signature for printName.


println("Step 1: How to define an abstract class called Donut")
 abstract class Donut(name: String) {

   def printName: Unit

}

NOTE:

  • Any class which extends the abstract class Donut will have to provide an implementation for the printName method.

2. How to extend abstract class Donut and define a case class called VanillaDonut

To create a sub-class of the abstract Donut class from Step 1, you have to use the extends keyword, pass-through the name constructor parameter and also provide an implementation for the printName method.

 

As shown in the example below, the case class VanillaDonut extends the abstract class Donut and also provides an implementation for the printName method.


println("\nStep 2: How to extend abstract class Donut and define a case class called VanillaDonut")
 case class VanillaDonut(name: String) extends Donut(name) {

   override def printName: Unit = println(name)

 }

NOTE:

3. How to extend abstract class Donut and define another case class of Donut called GlazedDonut

Similar to Step 2, let's create another case class named GlazedDonut which extends the abstract Donut class.



println("\nStep 3: How to extend abstract class Donut and define another case class called GlazedDonut")
 case class GlazedDonut(name: String) extends Donut(name) {

   override def printName: Unit = println(name)

 }

NOTE:

  • Similar to Step 2, we did not have to create a Companion Object for the GlazedDonut class.

4. How to instantiate Donut objects

Let's create two Donut objects, one using the VanillaDonut class and the other one using the GlazedDonut class. Note that we are specifying the type for both vanillaDonut and glazedDonut to be of base type Donut.


println("\nStep 4: How to instantiate Donut objects")
val vanillaDonut: Donut = VanillaDonut("Vanilla Donut")
vanillaDonut.printName

val glazedDonut: Donut = GlazedDonut("Glazed Donut")
glazedDonut.printName

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to instantiate Donut objects
Vanilla Donut
Glazed Donut

NOTE:

  • Since VanillaDonut and GlazedDonut are sub-classes of the base class Donut, they both have access to the printName method.

This concludes our tutorial on Learn How To Extend Case Class - Case Class Inheritance and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to define an abstract class called Donut
  • How to extend abstract class Donut and define a case class called VanillaDonut
  • How to extend abstract class Donut and define another case class of Donut called GlazedDonut
  • How to instantiate Donut objects

Tip

  • As we've seen in this tutorial, Scala provides support for the traditional Object Oriented approach regarding class inheritance by extending classes.
  • Avoid having a case class extend other case classes. Instead, encapsulate common behaviour in an abstract class - see Scala Documentation.
  • However, in Chapter 5, we will show how Scala provides greater flexibility to class inheritance by making use of mix-in with traits.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to add type parameters to classes in Scala.



 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: