33

I am reading the book The Java Programming Language, 3rd edition.

In chapter 3.5 , it illustrates the protected modifier with the following words:

More precisely, beyond being accessible within the class itself and to code within the same package, a protected member can also be accessed from a class through object references that are of at least the same type as the class that is, references of the class's type or one its subtypes.

The words makes me confused, in two aspects:

1. protected member can be accessed by code within the same package ? What I knew before is protected member can only be accessed by the subclass...

2. I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please?

Beth Lang
  • 1,889
  • 17
  • 38
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html. protected means that the attribute/ method can be accessed inside the package as well as sub types. – aishwarya Dec 26 '11 at 17:28
  • 1
    Here's a simple [cheat sheet](http://stackoverflow.com/a/33627846/276052) that explains `protected` and the other access modifiers. – aioobe Nov 10 '15 at 15:58
  • What you *thought* you 'knew before' isn't correct. The book is. – user207421 Jun 05 '20 at 10:13

6 Answers6

21
  1. Yes, protected members can be accessed from the class itself, subclasses of the class and also all classes in the same package of the class (doesn't matter if those are subclasses or not). If you didn't know that last part before, then you've just learned something new.

  2. It simply means that you can use those members; if a member is not accessible, it means you'll get a compiler error when you try to use it.

gnat
  • 6,213
  • 108
  • 53
  • 73
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • For 2nd statement: It doesn't throw errors when the `protected int status;` declared initially and access it later. Because it's default value is set to 0 automatically. So `status` will be always `0` when it is not accessible. – Janaka R Rajapaksha May 25 '16 at 10:38
  • @JanakaRRajapaksha the default value of member variables has nothing to do with accessibility. – Jesper May 25 '16 at 12:51
  • Assume i declared it from beginning. And trying to access it from unaccessible class(x) or method(x). Since it is not accessible, it will have it's default value when output it from a accessible place. Now according to your 2nd statement, I am expecting an error, but it doesn't. And it simply gives 0 as it's default value. So that codes in "x", useless. – Janaka R Rajapaksha May 25 '16 at 15:16
  • Sibling classes in the same package can access each others protected members via references. For example org.my.Derived1 and org.my.Derived2 that extend org.my.Base class can access each others protected members via references of the other type. However com.another.Derived3 that also extends org.my.Base cannot have its protected members accessed from Derived1 or Derived2 unless Derived3 is cast to com.my.Base. C# does not allow this, see https://learn.microsoft.com/en-us/archive/blogs/ericlippert/why-cant-i-access-a-protected-member-from-a-derived-class – Jose Quijada Sep 13 '21 at 23:32
15

In Java, protected means that the member can be accessed by any class in the same package and by subclasses even if they are in another packages.

Note

The protected access modifier is accessible within package and outside the package but through inheritance only

for example B (in another package) extends A and A has a protected int x; it can be use within the class B.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
  • @ZarEAhmer Could you please post your note as its own answer or a comment instead of an edit, and explain what you mean by "its instance variable"? – Solomon Ucko Nov 23 '22 at 22:30
2

1) Yes, protected members can be accessed by classes from the same package. That's the way Java works.

2) That means subclasses can access them.

Mario Marinato
  • 4,561
  • 3
  • 29
  • 49
0

I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please?

For example, you have an object A and an object B, both of the same class. Object A will be able to query the protected properties and methods of object B if it has a reference to it. The protected modifier is enforced at class level, not at object level. This can come in handy in some situations.

ChristopherS
  • 853
  • 4
  • 16
0

Here are the answers

  1. Yes. Protected members (instance variables and methods) of a class can be accessed by other classes within the same package as well as by any other class that extends this class containing the member to be accessed. In the same specification, they have also given the table where the access level is strictly increasing providing all the accesses allowed in the preceding level: private -> package -> protected -> public

  2. As protected members (instance variables / states and methods / behaviors) of a class X are inherited and visible as part of the sub classes of X, say Y1, Y2, Y3 and may be further down to the next levels, any object references of type X or Y1, Y2, y3 can be used to access that protected member.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
arunram
  • 623
  • 7
  • 11
0

Just think of it as between public and private. You can access everything from public classes, and less from private classes.

Jimmt
  • 852
  • 2
  • 11
  • 29