Scala Classes Flashcards

1
Q

Case Class

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Trait

A

Can be used to add methods onto different classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

abstract class

A

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 
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

anonymous class

A

A class that is made to to be used just once, using the abstract syntax without any parameters

abstract class Listener { def trigger }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

self type

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly