Scala Tutorial - Learn How To Create Classes And Objects In Scala
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.
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.