Scala Classes Flashcards
Case Class
Plain and Immutable class that is exclusively supposed to be defined by its constructor arguments.
Should be algebraic datatypes - should not be doing complex calculations inside the class.
Can be pattern-matched
If it extends another class, it must declare the parent class’s parameters or else it’s companion object will not be able to use them (thus the methods in companion obj will act like the parameters inherited from the parent are not there)
Trait
Can be used to add methods onto different classes
abstract class
A class that is designed to be extended, but not instantiated itself.
abstract class Car { val year: Int val automatic: Boolean = true def color: String }
anonymous class
A class that is made to to be used just once, using the abstract syntax without any parameters
abstract class Listener { def trigger }
self type
A type used on a trait to declare that it is only to be used with a certain class.
class A { def hi = “hi” }
trait B { self: A => def woo = "B" + hi }
class C extends B // ERROR: ILLEGAL INHERITANCE
class C extends A with B val obj = new C() obj.woo == "Bhi" // true