Scala Tutorial - Learn How To Extend Class - Class Inheritance

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

Overview

In this tutorial, we will learn the basics of Class Inheritance in Scala by creating an abstract class and then extending it to create other sub-classes.

 

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 sub-class of Donut 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 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 sub-class of Donut called VanillaDonut")
 class VanillaDonut(name: String) extends Donut(name) {

   override def printName: Unit = println(name)

 }

 object VanillaDonut {

   def apply(name: String): Donut = {
     new VanillaDonut(name)
   }

}

NOTE:

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

Similar to Step 2, let's create another sub-class named GlazedDonut for the abstract Donut class.



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

   override def printName: Unit = println(name)

 }

 object GlazedDonut {

   def apply(name: String): Donut = {
     new GlazedDonut(name)
   }

 }

NOTE:

  • Similar to Step 2, we've also created 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 Class - 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 sub-class of Donut called VanillaDonut
  • How to extend abstract class Donut and define another sub-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.
  • 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 apply inheritance to case 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: