Scala Tutorial - Learn How To Create Typed Class

By Nadim Bahadoor | Last updated: August 7, 2017 at 14:04 pm

Overview

In this tutorial, we will continue from what we've learned from the previous tutorials on Class Inheritance and Case Class Inheritance in Scala.

 

The examples in this tutorial will be a review of inheritance in Scala with classes and case classes which extend an abstract class. In addition, we will also define a class which expects a given type - type class.

 

NOTE: A class which expects a given type should not be confused with a Type-Class which supports ad-hoc polymorphism. We will show examples of Type-Classes under Chapter 5 on Traits.

 

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.

5. How to define a ShoppingCart type class which expects Donut types

The above steps should be familiar already if you have followed the previous tutorials on Class Inheritance and Case Class Inheritance.

 

What if you had to define a class which expects a particular type parameter? As an example, let us define a ShoppingCart class which expects a sequence of Donut types.


println("\nStep 5: How to define a ShoppingCart type class which expects Donut types")
class ShoppingCart[D <: Donut](donuts: Seq[D]) {

  def printCartItems: Unit = donuts.foreach(_.printName)

}

NOTE:

  • With the notation [D <: Donut], we are restricting only Donut types to be passed-through to the ShoppingCart class.

6. How to create instances or objects of ShoppingCart class

The example below shows how to instantiate a ShoppingCart class of type Donut.


println("\nStep 6: How to create instances or objects of ShoppingCart class")
val shoppingCart: ShoppingCart[Donut] = new ShoppingCart(Seq[Donut](vanillaDonut, glazedDonut))
shoppingCart.printCartItems

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


Step 6: How to create instances or objects of ShoppingCart class
Vanilla Donut
Glazed Donut

NOTE:

  • You will get compiler error if you try to instantiate a ShoppingCart class with a different type other than the Donut type.
  • As an example, you will get a compiler error if you tried to create ShoppingCart of type String

val shoppingCart2: ShoppingCart[Donut] = new ShoppingCart[String](Seq("Vanilla Donut"))

This concludes our tutorial on Learn How To Create Type Class 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
  • How to define a ShoppingCart type class which expects Donut types
  • How to create instances or objects of ShoppingCart class

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.
  • In the next tutorials, we will expand on what we've learned on type classes and go over variance in Scala - covariance and contra-variance.

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 specify covariance to type classes in Scala.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on