Home » JVM Languages » Scala » Scala: OOP basics

About Alexey Zvolinskiy

Alexey Zvolinskiy
Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

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.scala-logo

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.
 

Reference: Scala: OOP basics from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

2 comments

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

  2. examples are not working properly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


− 8 = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close