Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...