*
The moose likes Beginning Java and the fly likes Access modifier question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifier question..." Watch "Access modifier question..." New topic
Author

Access modifier question...

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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()


Thanks,
Shekar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No, it's not valid. Default-access members can be accessed only by classes in the same package.


[Jess in Action][AskingGoodQuestions]
michael dawson
Greenhorn

Joined: Jan 15, 2003
Posts: 1
Yes but only if it is in the same package(directory).
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access modifier question...