0
votes

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.

2

2 Answers

2
votes

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
2
votes

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