Lab 6
Lab 6
1
Agenda
1. Classes
1. Abstract Class & Abstract Method
2. Case Classes
3. Traits
2. Collections
1. Arrays
2. Lists
3. Maps
3. Hands-on
2
Classes
3
Abstract Class &
Abstract Method
An abstract class can
have both abstract
• A class which is declared with abstract keyword is known and
as abstract
non-abstract
class. (implemented)
methods.
abstract class Pet (name: String) {
var age: Int = 0 // concrete field we cannot
val animal: String // abstract field instantiate an
def printName(): Unit // abstract method abstract class
Abstract class maydirectly
be used using
as anew
def printAge(): Unit = { // concrete method operator
base class of some other class
println(age)
which implements the
}
unimplemented members.
}
4
Abstract Class & A class can
Abstract Method extend only one
abstract class.
6
Case Classes Setters
Fields
Other details
Getters
7
By default, all the
parameters in a
Case Classes case class
are public.
8
What if I want to
• Trait is something that is much more powerful and flexible than the Java
interface.
11
Traits
• Trait can include either concrete or abstract methods and fields.
trait Shape {
var color = "red" //Concrete field
val shapeName:String //abstract field
circle1.printShapeInfo()
println("Circle Area: " + circle1.getArea())
}
Shape name: Circle
}
Shape color: red
Shape radius = 5.0
Circle Area: 78.53981633974483
14
Traits trait Happy {
def mood(): Unit = println("I am very happy")
}
• Mixing multiple
trait Rich {
traits with the same val money: Long = 10000000L
class or object }
} 15
Collections
16
Collections
• Scala has a very rich collections library, located under the
scala.collection package.
17
Arrays
• Arrays in Scala are mutable, indexed collections of values.
val array: Array[Int] = Array(1, 2, 3) 1
println(array(1)) // 2
array(1) = 5 5
println(array(1)) // 5 3
array.foreach(x=>println(x))
val array2 = array.reverse
array2.foreach(println)
3
5
1
18
Lists
• Scala lists internally represent an immutable linked list.
val emptyList: List[Int] = List() // Empty List
var reservedwordsList: List[String] = List("if", "while", "for", "def")
19
List
• Append an item to the end of the list.
reservedwordsList = reservedwordsList :+ "val"
println(reservedwordsList) // List(if, while, for, def, val)
20
Maps
println(map.get("Algorithms")) // Some(C+)
22
Hands-on
23
• Create a person class as an abstract class. the class has
two attributes (name and gender). The attributes are
constant and cannot be accessed outside the class
Hands-on • Person class has a primary constructor that takes the
name and the gender.
• Person class has an abstract method called work and
abstract field nationality
• Create an employee class extends the person class.
• Employee class has a primary constructor that takes
name, gender , and empid as a parameters.
• Override the abstract field.
• Override the work function: if the empid equals 1 then
print “working”. If it equals 0, print “not working”.
• Create an object of employee and call the work function.
24
Solution
25
Solution
26
Thank you.
27