This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How to restrict access to a class hierarchy

No replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
I have a package p, containing an abstract class A with abstract method m, and a number of concrete subclasses (direct or indirect) of A, call them B, C, etc., that implement m. m must be visible within the class hierarchy, so if b is an instance of B and c is an instance of C, then b should be able to invoke c.m and vice versa. m is not accessed outside of instances of subclasses of A.
I'd like to figure out an appropriate access modifier to restrict m's visibility to within A's class hierarchy. Currently I'm doing "private[p] def m..." which accomplishes the really important part, that of ensuring m is not visible to users of the package. But I'm wondering if it's possible to tighten this up even further. I was just reading the appropriate section in "Programming in Scala 2", and "protected" will not do the trick since it disallows b invoking c.m. I've experimented with a few other things, which have not worked, so was wondering if this is possible.
Thanks,Ken

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland