0

In Java, the protected keyword limits the visibility of a field or method to the subclasses and the whole package.

Is there a way to limit the visibility only to subclasses, in a static way (i.e. at compilation time, without integrating runtime access control logic within the method)? In other words, is there a way to simulate the behaviour of a (friendless) C++-like protected modifier?

Eusebius
  • 531
  • 6
  • 24

2 Answers2

3

No, unfortunately there isn't any. As you have said, protected gives access also to classes in the same package.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
2

Short answer : no. Just exclude of the package the classes you don't want to be able to see the methods.

Dici
  • 25,226
  • 7
  • 41
  • 82