• 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

Access modifier question...

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we access a variable or a method which has default access in a class which extends the class containing the above from other package.
ex:
package pkg1;
class A{
void put(){};
}
package pkg2;
class B extends A{
public static void main(Strring[] s){
new B().put();
}
}
is the above valid?? can we access the method put()
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not valid. Default-access members can be accessed only by classes in the same package.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but only if it is in the same package(directory).
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the case if the package p1 lies in package p2 and then the variable is declared default. if the variable or method is declared in package p1 class A and there is a package p2 in pacakage p1 and a class B in package p2. can the class B access the default variable of class A
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps surprisingly, there's no special relationship between the classes in package p1 and those in a package p1.p2 . There's absolutely no difference between p1.p2 and some other package p3 as far as access control is concerned: being in a "subpackage" is just the same as being in a completely unrelated package.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Friedman-Hill (or anyone else that knows),
I've always wondered...why doesn't Java implement the idea of "protected private" access, where a method with both of those access modifiers would be accessible only to subclasses and no other class? I heard this idea was on the drawing board when the language was being designed but didn't go in.
I have never been able to figure out why Java treats classes in the same package with more privilege than subclasses of a different package. I can't say why this bothers me, but it seems on its head. Do you have any thoughts on this?
Thanks!
sev
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sever oon:
Mr. Friedman-Hill (or anyone else that knows),
I've always wondered...why doesn't Java implement the idea of "protected private" access, where a method with both of those access modifiers would be accessible only to subclasses and no other class?


How is that different from protected access? From the way I understand it, protect access grants access to subclasses and no others.
Layne
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected access also automatically grants package (default) access. I think what he wants is something that's available only to subclasses and not to other classes that just happen to be in the same package.
I would somewhat prefer that too. As for why it's not there, I think it's because the C++ definition of 'friend' is a bit complex, and they wanted Java to be a simple, easy to learn language, and were willing to give up a certain amount of data hiding safety for that goal.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic