Over a million developers have joined DZone.

Scala: OOP basics

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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:

1.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)

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

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 theprimary 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)


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.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}