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.
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

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 + 6 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books