Scala Tutorial - Learn How To Create Classes And Objects In Scala

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:15 pm

Overview

In this tutorial, we will learn how to create classes with input parameters and functions. In turn, we will show how to instantiate these classes into objects.

Steps

1. How to define a simple class to represent a Donut object

Let's start with defining a simple class which will represent a Donut object with name and productCode properties.

 

In addition, we will also have a print() function which will print the name and productCode for a given donut object. If you are not familiar with creating functions in Scala, you can refer back to the Functions Tutorials.


// "Step 1: How to define a simple class to represent a Donut object")
class Donut(name: String, productCode: Long) {

 def print = println(s"Donut name = $name, productCode = $productCode")

}

 

 

2. How to create instances of Donut class

If you have used another programming language like Java or .NET in the past, you should be familiar with using the new keyword to create an instance of a class - that is create a new Object of type Donut.


println("\nStep 2: How to create instances of Donut class")
val glazedDonut = new Donut("Glazed Donut", 1111)
val vanillaDonut = new Donut("Vanilla Donut", 2222)

 

 

3. How to call the print function for each of the donut object

Next, let's call the print() function from our donut objects as shown below.


println("\nStep 3: How to call the print function for each of the donut object")
glazedDonut.print
vanillaDonut.print

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


Step 3: How to call the print function for each of the donut object
Donut name = Glazed Donut, productCode = 1111
Donut name = Vanilla Donut, productCode = 2222

 

4. How to access the properties of class Donut

What if you needed to access the name or productCode properties on the donut objects?


println("\nStep 4: How to access the properties of class Donut")
glazedDonut.name
glazedDonut.productCode

NOTE:

  • You will get a compiler error if you try to access either the name or productCode properties from the donut objects.
  • Sure you could define getter functions to expose these properties, but in upcoming tutorials we will show how to make use of case class to automatically provide accessors for your objects.

This concludes our tutorial on Learn How To Create Classes And Objects 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:

  • How to define a simple class to represent a Donut object
  • How to create instances of Donut class
  • How to call the print function for each of the donut object
  • How to access the properties of class Donut

Tip

  • In the next tutorial, we will go over Companion Objects which will allow you create objects without having to use the new keyword.
  • We will also go over case classes which is a feature of Scala that will help you reduce boiler plate code by automatically generating accessors functions, hashCode and equals() methods.

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 Companion Objects.

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