Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Access Modifiers and Package 's

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//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
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic