- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to restrict access to a class hierarchy
Sat, 2011-06-18, 18:36
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
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