Introduction to Scala Classes

After you have learnt about Scala Variables, Methods and Tuples, you can now learn about Scala Classes.
What is a Scala Class?
For those of you who are familiar with Object Oriented Languages like Java, this is straightforward. For the people more new to the field of computer science, a class is a blueprint to create Objects.
The vocabulary needed here:
- Class: Blueprint to instantiate objects.
- Object: One instance of a class.
- Instantiate: Action of using the class blueprint to create a usable object in the code
- Parameters: Values that you give to the class to create an object
- Fields: Values that can be accessed from an object
- Methods: Functions that an object can perform
If this is all new to you, read the Wikipedia article on Classes in Computer Science before moving forward.
How to declare a Scala Class
Now that we know what it is, how do we use it ?
Which one of the classic example should we do? Animals
, Person
, House
, Car
? Let’s do BoardGame
instead.
class BoardGame( ) { }
This is the most simple class you could make, it has no parameters, no methods, no fields.
How do we instantiate the class to make an object?
We have to use the keyword new
:
val myBoardGame = new BoardGame()
With parameters
Now, let’s add a few parameters:
class BoardGame( name: String, numberOfPlayers: Int ) { }
We added two parameters to our class: name
which is a String
and numberOfPlayer
which is an Int
.
To create it, we use the new
keyword, like above but this time, we need the parameters:
val notCopyrightedGame = new BoardGame("No copyright", 4)
With methods
We can now add a method to our class:
class BoardGame( name: String, numberOfPlayers: Int ) { def canPlay(numberOfFriends: Int): Boolean = { this.numberOfPlayers >= numberOfFriends } }
And to use it:
notCopyrightedGame.canPlay(12)
This will return false, as 12 is greater than 4.
With fields
You can also add fields:
class BoardGame( name: String, numberOfPlayers: Int, timeOfGameInMinutes: Double ) { val playTimePerPlayer: Double = timeOfGameInMinutes / numberOfPlayers }
And to use it:
notCopyrightedGame.playTimePerPlayer
Private
You can use the keyword private
to make methods or fields inaccessible to the outside world.
private class BoardGame( name: String, numberOfPlayers: Int, timeOfGameInMinutes: Int ) { private val playTimePerPlayer: Double = ??? private def canPlay(numberOfFriends: Int): Boolean = ??? }
It can be used in several places, first, to make the class itself private
, you will see how it can be used in later articles.
You can make the fields private
if you are storing temporary values maybe for more complex methods.
And, you can make the methods private
, this is usually the case when you have helper
methods, they are methods which would help you make your more complex method more readable.
???
On a side note ???
is part of the Scala language, it allow you to set a method or value as not implemented. If you call this method in this state, you will get an error. You can read this post from the creator of Scala himself.
Companion Object
Keep in mind the term Companion object
it will come back in later articles.
Practical example of explicit Scala Class
Let’s go through a short example to practice what we just learnt.
Feel free to play with it.
If this does not work, you can directly go to Scastie.
What to remember?
A few things to remember about Scala Classes:
class
is the keyword to start the definition of a class- Parameters are set between
(
and)
- Methods and fields go between
{
and}
- You use the same
def
to start defining a method - You can use the same
val
andvar
to declare fields and evenlazy
private
allow you to hide methods and fields from the world outside the class???
allow you to not implement a method right away
Conclusion
You are now able to understand how to use classes in Scala code.
Feel free to leave comments if you have any questions.