As a hybrid post-functional language, Scala allows you to write OO code and create classes as well. Right now we will learn how to create classes and functions inside classes, and also how to work with traits, which are similar to Java interfaces in concept but way more powerful in practice.
We will see a simple Scala class in Scala REPL as follows:
$ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala> class Calculator { | def add(a: Int, b: Int): Int = a + b | def multiply(n: Int, f: Int): Int = n * f | } defined class Calculator scala> scala> val c = new Calculator c: Calculator = Calculator@380fb434 scala> c.add(1,2) res0: Int = 3 scala> c.multiply(3,2) res1: Int = 6 scala>
At first glance, the preceding code looks like Java. But let's add constructors, getters, and setters, and then you can see how much we can accomplish with just a few lines of code.
Following is a Scala plain old Java object in Scala REPL:
$ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala> class Person( | @scala.beans.BeanProperty var name:String = "", | @scala.beans.BeanProperty var age:Int = 0 | ){ | name = name.toUpperCase | override def toString = "name: " + name + " age: " + age | } defined class Person scala> scala> val p = new Person("Diego",31) p: Person = name: DIEGO age: 31 scala> val p1 = new Person(age = 31, name = "Diego") p1: Person = name: DIEGO age: 31 scala> p.getAge res0: Int = 31 scala> p1.getName res1: String = DIEGO scala>
Constructors in Scala are just lines of code. You might realize that we get the name
variable, and apply a function to change the given name to upper case in the preceding example. If you want, you can put as many lines as you want, and you can perform as many computations as you wish.
On this same code, we perform method overriding as well, because we override the toString
method. In Scala, in order to do an override, you need to use the override
operator in front of the function definition.
We just wrote a Plain Old Java Object (POJO) with very few lines of code in Scala. Scala has a special annotation called @scala.beans.BeanProperty
, which generates the getter and setter method for you. This is very useful, and saves lots of lines of code. However, the target needs to be public; you can't a apply BeanProperty
annotation on top of a private var
or val
object.
Following is a Person class in Java:
package scalabook.javacode.chap1; public class JavaPerson { private String name; private Integer age; public JavaPerson() {} public JavaPerson(String name, Integer age) { super(); this.name = name; this.age = age; } public JavaPerson(String name) { super(); this.name = name; } public JavaPerson(Integer age) { super(); this.age = age; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }