tokola
tokola

Reputation: 257

Protected classes scala?

If I have an abstract class, and want it to have two methods that are implemented by subclasses, and I don't want the methods to be visible outside the subclasses, how would I do this? I tried making the abstract methods protected and the implemented ones private, but keep getting errors. I need the method to be visible to the subclasses, and visible to nothing else.

Upvotes: 0

Views: 633

Answers (2)

emre
emre

Reputation: 51

The trick is to make the def protected in the implementing class too, otherwise it will be public.

Upvotes: 2

dhg
dhg

Reputation: 52681

It's sort of unclear, but the protected keyword should do what you want:

abstract class A() { protected[this] def f(): Unit }
class B() extends A() { protected[this] def f(): Unit = { println("B.f()") } }

val b = new B()
b.f()  // error: value f is not a member of B

Upvotes: 2

Related Questions