aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected access" Watch "protected access" New topic
Author

protected access

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Assume all three files difined in different source files.
File1.java
package p1;
public class Super{
protected void say(String s){
S.o.p(s);
}
}
File2.java
package p1;
public class SubOne extends p1.Super{}

File3.java
package p2;
public class SubTwo extends p1.Super{
1. public static void main(String args[]){
2. p1.Super s=new p1.Super();
3.p1.SubOne s1=new p1.SubOne();
4.SubTwo s2=new SubTwo();
5. s.say("Super");
6. s1.say("SubOne");
7. s2.say("SubTwo");
}
}
Will the above program compile?


nss
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
FEI NG
From the previous discussions, there was a conclusion that in order to access a protected variable from different package u require an instance of the subclass to call it.
this does not apply for subclasses in same package.
The example was:
package pkg;
public class A{
protected int protectedVar;
}
import pkg.A;
public class B extends A{
void checkAccess(){
A aa=new A();
S.o.p(aa.protectedVar);-->compiler error
S.o.p(protectedVar);-->compiles
S.o.p(bb.protectedVar);--->compiles
}
public statc void main(String args[]){
B bb=new B();
bb.checkAccess();
}
and the actual answer given for my above question is that there is a compiler error in at s2,say("SubTwo");
Please could you explain the above as well as this question
Thanx in advance
Neha
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
The compiler says
say(java.lang.String) has protected access in p1.Super
say(java.lang.String) has protected access in p1.SubOne

Why says that ?? shouldn't those methods be visible ?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi,
problem here is very interesting. remember one thing for sure,
THE SCOPES DEFINED BY ACCESS MODIFIERS ARE TEXTUAL SCOPES.
meaning that when we say that protected member (variable/method) is visible to the subclass, it does ONLY mean i can refer to that member in my class definition (that is textually) anywhere in the subclass. it has NOTHING to do with the object of that subclass. so if we try to access it thru creating object of that sub class it fails!!! same applies to other modifiers.
here for the example in question,
-we have Super,SubOne in package p1 and SubTwo in package p2.
-and say() is protected. so, it means i can call say() method of parent anywhere from any mothod of subclasses in package p1 and package p2. but can't call it using creating objects of those subclasses in package p2.
i'm not able to explain this correctly though. but consider the following code and think whether it will give me an error or not,
class A {
private int i=10;

void show(A a) {
S.o.p(a.i);
}
static void main(String[] args) {
A a1 = new A();
A a2 = new A();
a1.show(a2);
}
}
---mind well that int i is private for a1 and a2 object :-)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Protected means that while you are DECLARING the subclass (you are writing the code that IS the subclass) then you can get at the protected stuff.
It does not mean that you can get at the stuff if you are USING an object of the subclass. (I know it muddles my brain occasionally too).


2.7.4 Qualified Names and Access Control
A protected member of an object may be accessed only by code responsible for the implementation of that object. To be precise, a protected member may be accessed from anywhere in the package in which it is declared and, in addition, it may be accessed from within any declaration of a subclass of the class type that contains its declaration, provided that certain restrictions are obeyed.


------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport


"JavaRanch, where the deer and the Certified play" - David O'Meara
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi,
Please could anyone explain referring to my previous 2 examples.
My first code is giving error at line 7.Why??
And this is contradicts my second question.
I still am cofused how the protected variable/method should be accessed in subclass in same package/different package.
Whether be using instance in the subclass/superclass or directly.
Regards
Neha
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Neha
Your code doesn't give compile error at
s2.say("SubTwo");
because the type of s2 is the same as the class in which this sentence ocurrs, and because this type is subtype of the one that declares the protected member this statement access.
your code gives compile error at
s.say("Super");
s1.say("SubOne");
because though they occur in a type that is a subtype of the one that declared the protected say method, the types of s and s1 are not the same (or a subclass) where they appear.
Please read JLS 6.6.2 because there is more to say about protected access.

SCJP2. Please Indent your code using UBB Code
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thnax Jose,
Your reply solves my second question too.
protected variable can be accessed from the subclass using the instance of the subclass or directly if the subclass is in the same package and if the subclass is in different package then u require instance of subclass to access it.
Am i ight Jose?
Regards
Neha
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes, but besides that the expression must ocurr in the "same" type as
the expression as JLS states.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Jose,
expression must be of same type.
i did not get you.
Could ne u please be a bit clear.
Regards
Neha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected access