从此醉
从此醉

Reputation: 117

Scala: abstract classes instantiation with trait?

This a example code in the book:

abstract class Check {
  def check():String = "Checked Application Details... "
}
trait EmploymentCheck extends Check{
  override def check():String = "Check Employment ... " + super.check()
}

val app = new Check with EmploymentCheck

Which make me confused is the new Check, How can we instantiation a abstract class ? And why it will work by with EmploymentCheck ?

Upvotes: 0

Views: 135

Answers (2)

Erik Kaplun
Erik Kaplun

Reputation: 38207

The type you are instantiating is Check with EmploymentCheck not Check, and Check with EmploymentCheck is not abstract because the abstract member check has been filled in by EmploymentCheck.

Upvotes: 1

user1804599
user1804599

Reputation:

new Check with EmploymentCheck generates an anonymous concrete subclass of Check. No abstract class is instantiated.

scala> app.getClass
res0: Class[_ <: EmploymentCheck] = class $anon$1

Upvotes: 4

Related Questions