Alexey Zvolinskiy

About 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Scala: OOP basics"

  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


nine − 5 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close