Scala Quick Notes :: Part - 4
Bhaskar S | 01/24/2015 |
Overview
In this part of the series, we will continue to look at the Object-Oriented capabilities of Scala such as Inheritence, Polymorphism, Abstract Class and Case Class.
Hands-on With Scala - IV
The following is the Scala program named Sample12.scala:
Executing the program Sample12.scala results in the output:
[1] Class name = class com.polarsparc.scala.LandVehicle [2] Class name = class com.polarsparc.scala.Bike [3] Class name = class com.polarsparc.scala.Car v1 = Vehicle-[Unknown>] v1 Class name = class com.polarsparc.scala.Vehicle v2 = Bike-[Bike <2>, BMW], name = Bike v2 is a type of LandVehicle ? true v2 Class name = class com.polarsparc.scala.Bike v3 = Car-[Car <4>, Tesla, 2015], make = Tesla, year = 2015 v3 Class name = class com.polarsparc.scala.Car v4 wheels = 4
The following section explains some of the aspects of the Scala program Sample12.scala:
The Scala equivalent of the Java LandVehicle.class is a to call classOf[LandVehicle]
Ex: printf("[1] Class name = %s\n", classOf[LandVehicle])
To get the class on an instance of an entity, call v1.getClass method on the instance
Ex: printf("v1 Class name = %s\n", v1.getClass)
To check if an instance of an entity is of a particular type T, use the template VARIABLE.isInstanceOf[T]
Ex: printf("v2 is a type of Vehicle ? %s\n", v2.isInstanceOf[LandVehicle])
When defining a sub-class constructor that extends a base-class constructor, do not use the keywords val or var for the fields from the base-class constructor
Ex: class LandVehicle(name: String, val wheels: Int = 2) extends Vehicle(name) { ... }
To perform a type cast on an instance of an entity to type T, use the template VARIABLE.asInstanceOf[T]
Ex: val v4 = v3.asInstanceOf[LandVehicle]
The following is the Scala program named Sample13.scala:
Executing the program Sample13.scala results in the output:
t1 = VisualTrigger-[Alert: Happy ***FLASH*** 800 lm>] Hello Happy!!! t2 = SoundTrigger-[Alarm: Panic ***SCREECH*** 100 db>] Hello Panic!!!
The following section explains some of the aspects of the Scala program Sample13.scala:
An abstract class in Scala is similar to the one from Java where a class needs to begin with the abstract keyword. One difference compared to Java, the abstract (unimplemented) methods dont need the begin with an abstract keyword
Ex: abstract class Trigger(val name: String) { val desc: String; def action: String; def message = { printf("Hello " + name + "!!!\n") } }
One can have uninitialized val or var fields in the abstract class. They need to be defined with the val or var keyword in the concrete class and initialized
Ex: class VisualTrigger(name: String, val lumens: Int) extends Trigger(name) { val desc = "Alert" ... }
The following is the Scala program named Sample14.scala:
Executing the program Sample14.scala results in the output:
s1 = Snack-[Bagel] s2 = Snack-[Waffle]
The following section explains one of the aspects of the Scala program Sample14.scala:
Notice that we did not use the new keyword when creating an instance of Snack. This is because we have implemented the apply method on the companion object which acts like a factory method for creating class instances.
The following is the Scala program named Sample15.scala:
Executing the program Sample15.scala results in the output:
c1 = com.polarsparc.scala.Contact1@5cad8086 c1.hashCode = 1554874502 c1.equals(c11) = false c2 = Contact2(Alice,alice@earth.com) c2.hashCode = -106199946 c2.equals(c22) = true c3 = Contact2(Bob,bob@space.com) c3.hashCode = -521222824 c3.equals(c33) = true Name: Alice, Email: alice@earth.com
The following section explains some of the aspects of the Scala program Sample15.scala:
Scala case class is a type of a class in which the compiler automatically generates a lot of the boilerplate code behind the scenes for methods like toString, equals, hashCode, apply, unapply, etc
The fields in the case class constructor arguments are val by default
The unapply method is invoked when we want extract the values of fields for an instance of a class in a match expression
Ex: c2 match { case Contact2(n, e) => printf("Name: %s, Email: %s\n", n, e) }
References