• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about member variable

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//File in /abc/def/Q.java:
package def;
public class Q {
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
}
//File in /abc/Sub.java:
public class Sub extends Tester {
}

//File in /abc/Tester.java:
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
}
}

Question:
I don't understand why q.protectedVar = 3; cause the compile error. Thanks.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Example:
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"
 
Romy Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. I got it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic