• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion about "direct access" to a protected field across package boundary.  RSS feed

 
Robert Kelvin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, why am I allowed “direct access” to an object's protected field, whose class is defined inside a different package?

Main.java, main package:

A.java, main package:

B.java, test package:

I thought that once a protected field was inherited, that field becomes private to all "outside callers". Yes, it can be inherited again by another sub class, but when I access the protected field in my example code, I am not using inheritance. Thanks a lot for any help on this topic.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Kelvin wrote:I thought that once a protected field was inherited, that field becomes private to all "outside callers".

Where did you read that?

Yes, it can be inherited again by another sub class, but when I access the protected field in my example code, I am not using inheritance.

You're not? What do you think 'B extends A' is?

And where is A defined? Try moving it to test, and see what happens.

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"protected" means: accessible from all classes in the same package, and from subclasses (no matter if they are in the same package or not).

A good overview of access specifiers can be found here: Controlling Access to Members of a Class
 
Robert Kelvin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:"protected" means: accessible from all classes in the same package, and from subclasses (no matter if they are in the same package or not).[/url]

My "calling code", which is in a different package to the one that defines class B, is not extending B. That's why I'm puzzled as to why I am allowed direct access to x, as in new B().x
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Kelvin wrote:
My "calling code", which is in a different package to the one that defines class B, is not extending B. That's why I'm puzzled as to why I am allowed direct access to x, as in new B().x


Your calling code is in the same package as the protected member you're accessing. Protected members can always be accessed from within the same package they are declared. If you put your calling code into a different package to A then you'll find it will fail to compile.

From the JLS 6.6.2. Details on protected Access


A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!