This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package visibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package visibility" Watch "package visibility" New topic
Author

package visibility

xwe
Greenhorn

Joined: Feb 22, 2001
Posts: 5
//file Tester.java
import def.Q;//line 1
public class Tester extends Q{ //line2
Q q = new Q();//line3
public void someMethod() {//line4
q.var = 1;//line5
}
}
//file def\Q.java
package def;
public class Q{
protected int var;
}
The above code has compilation error: "Can't access protected field var in class def.Q. def.Q is not a subclass of the current class." Can anybody explain to me why? thanks.

Rajesh Ugemuge
Greenhorn

Joined: Feb 23, 2001
Posts: 1
It never gives the error if we we access the protected field of class if it is subclass of that class weather it is same package or not
[This message has been edited by Rajesh Ugemuge (edited February 23, 2001).]
xwe
Greenhorn

Joined: Feb 22, 2001
Posts: 5
Yes, if i change line 5 to "var=1", it would compile fine. but if access through an instance it doesn't compile, also the error message is confusing, it doesn't tell you exactly why it's wrong. i think "var" belongs to class Q, why instance of Q can't access it?
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
xwe, this is a real tricky one, but stay with me and all will be understood.
The reason you get your compilation error is this:
The class attempting to access the protected member is Tester while the type of the reference to the object being accessed is Q.
Q is not the same as, nor a subclass of, Tester, so the access is not allowed.
The Java Programming Language By Arnold & Gosling(http://java.sun.com/docs/books/javaprog/) explains this really well.
xwe
Greenhorn

Joined: Feb 22, 2001
Posts: 5
Thanks for the explain. So I can think this way, Tester is a subclass of Q, so it inherits the protected variable "var" from Q. When instance of Q tries to access "var" of the Tester class, because Q is not the subclass of Tester and not the same package as Tester, the access is denied. Am I right? Anyway, hope there won't be such questions in the real exam.
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
Thats about it.
Quote from book I mentioned in last post:

The reasoning behind the restriction is this: Each subclass inherits the contract of the superclass and expands that contract in some way. Suppose that one subclass, as part of its expanded contract, places constraints on the values of protected members of the superclass. If a different subclass could access the protected members of objects of the first subclass then it could manipulate them in a way that would break the first subclass's contract--and this should not be permissable.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Xwe,
Will you please read the Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package visibility
 
Similar Threads
Protected access
protected and anynomous class
Question on Protected
Package & modifier question
question about member variable