-1

Java's protected members declared in class are visible in the whole package. Why?

I simply want to share privacy between my class and it's subclasses but Java doesn't allow me.

What I am supposed to do?

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
user2591935
  • 299
  • 1
  • 14
  • Please post your code, how are you declaring these fields or methods? – Elliott Frisch Apr 09 '16 at 14:31
  • 2
    Live with the way java access modifiers work. – Robert Moskal Apr 09 '16 at 14:33
  • 3
    See [this table](http://stackoverflow.com/a/33627846/276052) for a clear description of the scope of protected. *Why* the semantics of protected was implemented this way can most likely only be answered by the designers of the language. – aioobe Apr 09 '16 at 14:37
  • Which part of tutorial/documentation makes you think that it should behave differently? – Pshemo Apr 09 '16 at 14:38

3 Answers3

1

protected are sharing also between package members, so you could move your class and its children to a separate package.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
0

protected Members are Visible to the package and all subclasses (protected). So, Why so surprised ? .

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

This is its intended behavior. All fields that are declared as protected are visible within the class, the package and all subclasses, including those outside the package.

Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48