Simon Simon - 1 month ago 6
Scala Question

Scala: are all the methods of a Trait imported or just the one that are used?

I'm wondering if all the methods of a

Trait
are imported in a class which extends this
Trait
or just the used ones?

And with dependency injection?

Answer

Except the methods which are marked private every other method will be available with the extending entity. The extending entity can choose to override or leave unimplementated, in which case the children of this entity has to deal with it.

trait A {
 private def a: Int = 1 //not available
 protected def b: Int = 2 //available inside extending entity
 def d: Int = 3 //available everywhere 
}

class B extends A {}
val x = new B()

x.a //error

x.b //error

x.d //prints 3

Coming to dependency injection

Lets say A and B are two interfaces or traits which do not hold parent/child relationship.

If you inject one entity into other. All public methods of that entity can be accessed by other

Comments