It's possible to do inheritance in Scala as well. For such a task, you use the operator extend
after the class definition. Scala just allows you to extend one class, just like Java. Java does not allow multiple inheritance like C++. However, Scala allows it by using the Mixing technique with traits. Scala traits are like Java interface, but you can also add concrete code, and you are allowed to have as many traits as you want in your code.
Following is a Scala inheritance code 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> class LowerCasePerson(name:String,age:Int) extends Person(name,age) { | setName(name.toLowerCase) | } defined class LowerCasePerson scala> scala> val p = new LowerCasePerson("DIEGO PACHECO",31) p: LowerCasePerson = name: diego pacheco age: 31 scala> p.getName res0: String = diego pacheco scala>
Scala does not make constructors inheritance like Java. So you need to rewrite the constructors and pass the values through a super class. All code inside the class will be the secondary constructor. All code inside parentheses ()
in the class definition will be the primary constructor. It's possible to have multiple constructors using the this
operator. For this particular implementation, we changed the default behavior and added new constructor code in order to make the given name lower case, instead of the default uppercase defined by the Person
superclass.
Following is a Scala traits sample code 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> trait Car defined trait Car scala> scala> trait SportCar { | val brand:String | def run():String = "Rghhhhh Rghhhhh Rghhhhh...." | } defined trait SportCar scala> scala> trait Printable { | def printIt:Unit | } defined trait Printable scala> scala> class BMW extends Car with SportCar with Printable{ | override val brand = "BMW" | override def printIt:Unit = println(brand + " does " + run() ) | } defined class BMW scala> scala> val x1 = new BMW x1: BMW = BMW@22a71081 scala> x1.printIt BMW does Rghhhhh Rghhhhh Rghhhhh.... scala>
In the preceding code, we created multiple traits. One is called Car, which is the mother trait. Traits support inheritance as well, and we have it with the SportCar
trait which extends from the Car
trait. The SportCar
trait demands a variable called brand, and defines a concrete implementation of the function run. Finally, we have a class called BMW
which extends from multiple traits -- this technique is called mixing.
Following is a Scala traits using variable mixing technique at 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> trait SportCar { | def run():String = "Rghhhhh Rghhhhh Rghhhhh...." | } defined trait SportCar scala> scala> val bmw = new Object with SportCar bmw: SportCar = $anon$1@ed17bee scala> bmw.run res0: String = Rghhhhh Rghhhhh Rghhhhh.... scala>
Scala is a very powerful language indeed. It's possible to add traits to a variable at runtime. When you define a variable, you can use the with
operator after the assignment. This is a very useful feature, because it makes it easier to make function composition. You can have multiple specialized traits and just add them in your variables as you need them.
Scala allows you to create the type
alias as well, this is a very simple technique which will increase the readability of your code. It's just a simple alias.
Following is a Scala type alias sample 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> type Email = String defined type alias Email scala> scala> val e = new Email("[email protected]") e: String = [email protected] scala>
When you are coding with Scala, it is highly recommended that you use the type
alias and traits for everything, because that way you will get more advantages with your compiler, and you will avoid writing unnecessary code and unnecessary unit tests.