Scala: OOP basics
The most part of time when I’m coding I use Java. So it’s my main programming language. It satisfies me in all aspects, but I notice that Java is too verbose sometimes. That’s why I have switched to Scala. So all free time I spend now for learning Scala. I’m going to publish some notes in my blog, hence you are welcome to visit new page of my blog which will be dedicated to Scala. The first serious article will be about Scala OOP.
Class declaration
OOP in Scala allows to create classes. But the process of declaration differs comparing with Java. Here is how we can do it in Scala:
class Person
Nothing hard. And it’s not strange, we just declared absolutely useless class. Let’s move further to look at how we can upgrade the code in order to make it more practical.
Primary constructor
I’m going to add some parameters into the Person class which need to be specified during object creation. As many of you may guess, I’m talking about primary constructor:
class Person(name: String, age: Int)
A Primary constructor can be called directly when we are creating an object. Here is an example:
val p1 = new Person("Alex", 24)
And also it can be called indirectly in an overloaded constructor. An overloading is out of topic and I’ll discuss this in the following posts.
Prefixed constructor parameters
Ok, we have already declared the Person class and looks like it can be more or less useful, since it has some initialization parameters passed in the primary constructor. But how we can interact with these parameters after the initialization?
scala> p1.name <console>:10: error: value name is not a member of Person p1.name
As you can see, the attempt to access the name parameter of the Person instance class causes error. That’s because the name parameter doesn’t has any prefix in the primary constructor declaration. Hence the name and the age parameters are declared as private and they are not accessible outside the Person class.
class Person(val name: String, var age: Int)
I modified the Person class declaration. Now the name parameter will be supplied with a getter, because it is prefixed with val, and the age parameter will be supplied with a setter in addition to setter, because it is prefixed with var.
scala> val p2 = new Person("Bobby", 25) p2: Person = Person@30374534 scala> p2.name res1: String = Bobby scala> p2.name = "Bob" <console>:9: error: reassignment to val p2.name = "Bob" scala> p2.age res2: Int = 25 scala> p2.age = 26 p2.age: Int = 26
With help of val and var prefixes inside a primary constructor you are able to make classes more practical.
Class body
Let’s move further with OOP basics in Scala. Now I want to show how Scala classes can be more practical when they contain some additional functionality inside a class body.
class Person(val name:String, var age:Int) { def introduce() = println(s"Hi, my name is ${name}, I'm ${age} years old") }
Now an instance of the Person class is more social and he (she) may introduce himself to someone. Try the code:
val p3 = new Person("Jhon", 33) p3.introduce
Summary
Well in the post I tried to make an overview of the most simplest and essential basics of Scala OOP. In my oppinion, Scala OOP is more complex than Java OOP. Maybe even not more complex, but it different and it definitely requires some time to get used to it.
In the “Prefixed constructor parameters”, it should be “with a setter in addition to getter” instead of “with a setter in addition to setter”. just a writing error.
examples are not working properly.