File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about member variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about member variable" Watch "question about member variable" New topic

question about member variable

Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
//File in /abc/def/
package def;
public class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
//File in /abc/
public class Sub extends Tester {

//File in /abc/
import def.Q;
public class Tester extends Q {
public void someMethod() {
Q q = new Q();
Sub sub = new Sub();
// First, try to refer to q's memebers.
q.privateVar = 1; // compiler error
q.packageVar = 2; // compiler error, not in same pacage
q.protectedVar = 3; // compiler error
q.publicVar = 4; // fine
// Next, try to refer to this object's members
// supplied by class Q.
privateVar = 5; // compiler error
packageVar = 6; // compiler error
protectedVar = 7; // fine
publicVar = 8; // fine
// Next, let's try to access the members of
// another instance of Tester.
Tester t = new Tester();
t.privateVar = 9; // compiler error
t.packageVar = 10; // compiler error
t.protectedVar = 11; // fine
t.publicVar = 12; // fine
// Finally, try to refer to the members in a
// subclass of Tester.
sub.privateVar = 13; // compiler error
sub.packageVar = 14; // compiler error
sub.protectedVar = 15; // fine
sub.publicVar = 16; // fine

I don't understand why q.protectedVar = 3; cause the compile error. Thanks.
Aiping Zhou

Joined: Sep 06, 2003
Posts: 24
Because class abc.Tester is in package abc, and class abc.def.Q is in different package def. The protected member can be accessed by classes in same package and kids. (Note: kid can access it only by inheritance.)
Basically, out of the package, no one can use obj.protectedMember code to access protected member. Here obj means the reference to the class, which has those protected members you want to access.
[ September 30, 2003: Message edited by: Aiping Zhou ]
Sachin Tendulkar

Joined: Sep 20, 2003
Posts: 27
I am a newbie to this forum and have posted a few questions. I have read about protected access from the JLS and here is what I could make of it. A request to all readers who may find mistakes with my explanation -- Do correct me if I am wrong. I have tried my best to understand this concept and here is what I have.
public class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
We have a hierarchy as follows
Q <--- Tester <--- Sub.
According to the JLS - fields in a class that are declared protected are accessible outside the class' package only in subclasses of the class declaring the field and only when (note this point) these fields are of objects that are being implemented by the code that is accessing them.
In your case the field protectedVar is declared protected in class Q. It is therefore visible to all classes in the package def as well as to subclasses in other packages.
However for accessibilty the second clause says that the code trying to access these fields should implement the same.
In your case the field is implemented by class Q. Tester is a subclass of Q hence Tester inherits the field from Q. But Tester has no role in the implementation of the field in Q. Hence accessing the field of Q using an instance of Q gives a compile-time error but accessing the field using an instance of Tester does not because every Tester class inherits and hence implements its own protected field.
Class Sub is a subclass of Tester. So Sub inherits the protectedVar field from Tester. Note that in this case the Tester class is a superclass of Sub and hence it it plays a role in implemeting the Subclass. That is the reason why you can access the protected variable of Sub from the class Tester but not that of Q from Tester.
In short a protected variable inherited by a subclass is accessible to its superclass through an object of the subclass but not to a subclass through an object of its super class.
This might sound confusing but go through that line a few times and everything will make sense.
For the same reason if you introduce a new protected variable "protectedVarOnlyInSub" in the class Sub and try to access it from the Tester class you will get a compile time error. This is because the new variable is not implemented by the Superclass and hence is not accessible to it.
Please let me know if what I have presented here makes any sense and correct me if I have completely misunderstood this concept.
Asha Sat

Joined: Sep 29, 2003
Posts: 22
Here is the rule for protected members
a) protected members can be accessed by other classes in the same package
b) For subclasses outside the package , the protected member can be accessed only through inheritance. A subclass outside the package cannot access a ptotected member using a reference to the instance of the super class.
package p1;
public class A{
protected int acctNo;
package p2;
public class B extends A{
private void method1(){
A newclassA = new A().acctNo; // compile error
Logical reason: When one class extends another class , instances of subclass wil contain their own copy of all members of super class and sub class. Which means , instance of class B has a copy of variable acctNo, whose visibility is protected so the subclass can access its variable acctNo. This confirms rule b) You can access the acctNo through inheritance means "subclass copy can be accessed by subclass B " . In your example q is the instance of super class , so class B does not have access to super class protected members using super class instance.
Just remember the rule b and INHERITANCE.
There is one more weirdo scenario where "if the subclass creates another instance of subclass say B1 , subclass B has access to B1's acctno"

{ Winners do what losers don't do }
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
Thank you all. I got it.
I agree. Here's the link:
subject: question about member variable
Similar Threads
why doesn't this code run
Protected access
protected and anynomous class
Package & modifier question
Question on Protected