File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifiers and Package 's 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 "Access Modifiers and Package Watch "Access Modifiers and Package New topic
Author

Access Modifiers and Package 's

Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
//file 1
package A;
public class SuperClass{
protected int iInt;
}

//file 2
package B;
public class SubClass extends SuperClass{
SuperClass objectRefA = new SubClass();
void aMethod(){
System.out.println(objectRefA.iInt);// line 1
}
}
why is does it give problems accessing protected members using superclass reference at line 1.
"subclasses in the packages different from their superclass can only access protected members of the super class in their inheritance hierarchy"
I dont get what this means.. can someone expalin this to me.
Thanks,
reshma


SCJP 1.4, SCWCD 1.4
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
The sub class in a different package can directly access the protected variable without dot operator. But if the dot operator is used compilation error results


Groovy
Amrita Gandhi
Greenhorn

Joined: Feb 19, 2003
Posts: 1
hello reshma,
According to java language specifications, you can access the protected members of the superclass, but not through the object of superclass and it is irrespetive of the package.
Gurucharan Murudeshwar
Greenhorn

Joined: Feb 19, 2003
Posts: 15
Originally posted by Reshma Pai:
//file 1
package A;
public class SuperClass{
protected int iInt;
}

//file 2
package B;
public class SubClass extends SuperClass{
SuperClass objectRefA = new SubClass();
void aMethod(){
System.out.println(objectRefA.iInt);// line 1
}
}
why is does it give problems accessing protected members using superclass reference at line 1.
"subclasses in the packages different from their superclass can only access protected members of the super class in their inheritance hierarchy"
I dont get what this means.. can someone expalin this to me.
Thanks,
reshma


Of course, the SuperClass is in "Package A" and the SubClass is in "Package B". So, to use a protected variable of the SuperClass, we have to extend from it in the "same" package !
Cheers,
gurucharan
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Replace following line:
SuperClass objectRefA = new SubClass();
with:
SubClass objectRefA = new SubClass();
it will work.
Rational is that protected members are available via the sub classes/objects only NOT via the classes/objects where they are declared.
Anshul Chhabra
Greenhorn

Joined: Feb 18, 2003
Posts: 10
Most of the points are covered in the answers above.
I found an interesting sentence in JLS regarding access control, I quote:
"Note that accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers."
To me, this means that while deciding access, the compiler is not (and in fact cannot) figure out the dynamic scenario - which is whether they belong to the same instance or not, etc.
When they _have_ to rely on compile time static information - namely type and modifiers, the behaviour becomes easier to understand.
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Hi All,
Thanks a lot for tyring to help me ... but i am still confused...
i need Clarification why a subclass in a different package than its super class cannot access the protected members of the super class using the super class reference ... while this is possible to do from subclass which resides in the same package as its parent.
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Hi All,
I went through the specification for protected access and have got a clear understanding. I paste will below an abstract of the same.
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. Let C be the class in which a protected member or constructor is declared and let S be the subclass of C in whose declaration the use of the protected member or constructor occurs. Then:
If an access is of a protected member (field or method), let Id be its name. Consider then the means of qualified access:
If the access is by a field access expression of the form super.Id, then the access is permitted.
If the access is by a qualified name Q.Id, where Q is a TypeName, then the access is permitted if and only if Q is S or a subclass of S.
If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
Otherwise, if an access is of a protected constructor:
If the access is by a superclass constructor invocation super(. . .), then the access is permitted.
If the access is by a class instance creation expression new T(. . .), then the access is not permitted. (A protected constructor can be accessed by a class instance creation expression only from within the package in which it is defined.)
If the access is by an invocation of the method newInstance of class Class (�20.3.6), then the access is not permitted.
Thanks to all for trying to help me out
reshma
Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
Hello Reshma,
Protected modifier only can be accessed from different package's class only if there is a "IS-A" relationship and you cant access if you have "HAS-A" relationship.
In your program, you are exteding super class which means u have "IS-A" relationship, so far it is fine. but while you are trying to access the variable, you are using reference of super class which falls on "HAS-A" relationship and gives you error.
If you will try to access with the subclass reference itself, you will be accessing the protected variable with "IS-A" relation itself bcoz in this object, superclass comes as a inherited which is perfectly fine.
hope it clears!


"Walking on water and building IT Architecture from <br />specification are easy if and only if both are frozen"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers and Package 's