문제

For example, I have two classes:

class Foo
{
  protected void say() {...};
}

class Bar extends Foo {.....}

Can I somehow allow the "say" method to be called only from the Bar and other derived classes, but not from the other classes in Foo's package.

도움이 되었습니까?

해결책

No, protected mean, protected member can be accessed in the same package and all subclasses in any package. If you want to restrict the access in package level, move that class into a separate package.

다른 팁

There's no such accessor in Java that meet your needs, and AFAIK it's not possible to implement that restriction (in a simple way, that is, and besides Kugathasan's solution).

There isn't a way how you can do this in java.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top