Scala Tutorial - Learn How To Create And Extend Trait In Scala

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

Overview

In this tutorial, we will learn how to create trait which defines methods that a consuming class should implement. This is similar to implementing an interface if you come from an Object Oriented background and have used Java or .NET in the past.

Steps

1. Create a trait which will define the methods for a data access layer

Let's start with defining a simple trait called DonutShoppingCartDao and it will provide the method signatures to represent a Data Access Layer.


println("Step 1: Create a trait which will define the methods for a data access layer")
trait DonutShoppingCartDao {

 def add(donutName: String): Long

 def update(donutName: String): Boolean

 def search(donutName: String): String

 def delete(donutName: String): Boolean

 }

NOTE:

  • You create a trait in Scala by making use of the keyword trait.

2. Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods

If you have used another programming language like Java or .NET in the past, you should be familiar with using the extends keyword to implement an interface. Similarly, to implement the trait DonutShoppingCartDao from Step 1, you will need to make use of the extends keyword.


println("\nStep 2: Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods")
class DonutShoppingCart extends DonutShoppingCartDao {

 override def add(donutName: String): Long = {
  println(s"DonutShoppingCart-> add method -> donutName: $donutName")
  1
 }

 override def update(donutName: String): Boolean = {
  println(s"DonutShoppingCart-> update method -> donutName: $donutName")
  true
 }

 override def search(donutName: String): String = {
  println(s"DonutShoppingCart-> search method -> donutName: $donutName")
  donutName
 }

 override def delete(donutName: String): Boolean = {
  println(s"DonutShoppingCart-> delete method -> donutName: $donutName")
  true
 }
}

 

3. Create an instance of DonutShoppingCart and call the add, update, search and delete methods

We can now create an instance of DonutShoppingCart and call the corresponding add, update, search and delete methods.


println("\nStep 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
val donutShoppingCart1: DonutShoppingCart = new DonutShoppingCart()
donutShoppingCart1.add("Vanilla Donut")
donutShoppingCart1.update("Vanilla Donut")
donutShoppingCart1.search("Vanilla Donut")
donutShoppingCart1.delete("Vanilla Donut")

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


Step 3: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
DonutShoppingCart-> add method -> donutName: Vanilla Donut
DonutShoppingCart-> update method -> donutName: Vanilla Donut
DonutShoppingCart-> search method -> donutName: Vanilla Donut
DonutShoppingCart-> delete method -> donutName: Vanilla Donut

 

4. Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao

Since our DonutShoppingCart class extended the trait DonutShoppingCartDao, you can also assign the type of the DonutShoppingCart object to the trait DonutShoppingCartDao as follows:


println("\nStep 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao")
val donutShoppingCart2: DonutShoppingCartDao = new DonutShoppingCart()
donutShoppingCart2.add("Vanilla Donut")
donutShoppingCart2.update("Vanilla Donut")
donutShoppingCart2.search("Vanilla Donut")
donutShoppingCart2.delete("Vanilla Donut")

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


Step 4: Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao
DonutShoppingCart-> add method -> donutName: Vanilla Donut
DonutShoppingCart-> update method -> donutName: Vanilla Donut
DonutShoppingCart-> search method -> donutName: Vanilla Donut
DonutShoppingCart-> delete method -> donutName: Vanilla Donut

This concludes our tutorial on Learn How To Create And Extend Trait In Scala 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:

  • Create a trait which will define the methods for a data access layer
  • Create a DonutShoppingCart class which extends the trait from Step 1 and implements its methods
  • Create an instance of DonutShoppingCart and call the add, update, search and delete methods
  • Create an instance of DonutShoppingCart and assign its type to the trait DonutShoppingCartDao

Tip

  • In upcoming tutorials in this Chapter, we will also show how to use traits to build some pure Functional Programming constructs such as Monoids and Functors and much more!

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how create Traits with type parameters.

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: