Scala Pgm Notes - karbigdata/BigDnotes GitHub Wiki
Method Notations in Scala

package lectures.part2oop
/**
* Created by Daniel.
*/
object MethodNotations extends App {
class Person(val name: String, favoriteMovie: String, val age: Int = 0) {
def likes(movie: String): Boolean = movie == favoriteMovie
def +(person: Person): String = s"${this.name} is hanging out with ${person.name}"
def +(nickname: String): Person = new Person(s"$name ($nickname)", favoriteMovie)
def unary_! : String = s"$name, what the heck?!"
def unary_+ : Person = new Person(name, favoriteMovie, age + 1)
def isAlive: Boolean = true
def apply(): String = s"Hi, my name is $name and I like $favoriteMovie"
def apply(n: Int): String = s"$name watched $favoriteMovie $n times"
def learns(thing: String) = s"$name is learning $thing"
def learnsScala = this learns "Scala"
}
val mary = new Person("Mary", "Inception")
println(mary.likes("Inception"))
println(mary likes "Inception") // equivalent
// infix notation = operator notation (syntactic sugar)
// "operators" in Scala
val tom = new Person("Tom", "Fight Club")
println(mary + tom)
println(mary.+(tom))
println(1 + 2)
println(1.+(2))
// ALL OPERATORS ARE METHODS.
// Akka actors have ! ?
// prefix notation
val x = -1 // equivalent with 1.unary_-
val y = 1.unary_-
// unary_ prefix only works with - + ~ !
println(!mary)
println(mary.unary_!)
// postfix notation
println(mary.isAlive)
println(mary isAlive)
// apply
println(mary.apply())
println(mary()) // equivalent
/*
1. Overload the + operator
mary + "the rockstar" => new person "Mary (the rockstar)"
2. Add an age to the Person class
Add a unary + operator => new person with the age + 1
+mary => mary with the age incrementer
3. Add a "learns" method in the Person class => "Mary learns Scala"
Add a learnsScala method, calls learns method with "Scala".
Use it in postfix notation.
4. Overload the apply method
mary.apply(2) => "Mary watched Inception 2 times"
*/
println((mary + "the Rockstar").apply())
println((+mary).age)
println(mary learnsScala)
println(mary(10))
}
Scala Inheritance
package lectures.part2oop
/**
* Created by Daniel.
*/
object Inheritance extends App {
// single class inheritance
sealed class Animal {
val creatureType = "wild"
def eat = println("nomnom")
}
class Cat extends Animal {
def crunch = {
eat
println("crunch crunch")
}
}
val cat = new Cat
cat.crunch
// constructors
class Person(name: String, age: Int) {
def this(name: String) = this(name, 0)
}
class Adult(name: String, age: Int, idCard: String) extends Person(name)
// overriding
class Dog(override val creatureType: String) extends Animal {
// override val creatureType = "domestic"
override def eat = {
super.eat
println("crunch, crunch")
}
}
val dog = new Dog("K9")
dog.eat
println(dog.creatureType)
// type substitution (broad: polymorphism)
val unknownAnimal: Animal = new Dog("K9")
unknownAnimal.eat
// overRIDING vs overLOADING
// super
// preventing overrides
// 1 - use final on member
// 2 - use final on the entire class
// 3 - seal the class = extend classes in THIS FILE, prevent extension in other files
}
Scala Abstraction
package lectures.part2oop
/**
* Created by Daniel.
*/
object AbstractDataTypes extends App {
// abstract
abstract class Animal {
val creatureType: String = "wild"
def eat: Unit
}
class Dog extends Animal { //inheritance from Abstract class (Animal)
override val creatureType: String = "Canine"
def eat: Unit = println("crunch crunch")
}
// traits
trait Carnivore {
def eat(animal: Animal): Unit
val preferredMeal: String = "fresh meat"
}
trait ColdBlooded
class Crocodile extends Animal with Carnivore with ColdBlooded {
override val creatureType: String = "croc"
def eat: Unit = println("nomnomnom")
def eat(animal: Animal): Unit = println(s"I'm a croc and I'm eating ${animal.creatureType}")
}
val dog = new Dog
val croc = new Crocodile
croc.eat(dog)
// traits vs abstract classes
// 1 - traits do not have constructor parameters
// 2 - multiple traits may be inherited by the same class
// 3 - traits = behavior, abstract class = "thing"
}
