ok, i'm currently in the throes of understanding how protected access works and i think i only need a nudge to push me over the edge. what i've got is a class in one package ("thispackage") that inherits from a class in another package ("otherpackage") and then tries to use a protected int in it's superclass. it seems to me that it should work but it doesn't. over in /com/otherpackage i've got a file called PubClassInOtherPkg.java and in it is:
over in com/thispackage i've got a file called Peach.java which contains the following code:
So, to sum up, the Peach object has a static method (main()) that creates a local object (ChildOfPubClassInOtherPkg co) and says to that local object, "hey, cough up the protected int you inherited from that public class in the other package." but the compiler refuses here, claiming: Peach.java:14 protI has protected access in com.otherpackage.PubClassInOtherPkg could someone please enlighten me? thanks.
No, this isn't correct. Protected access will only allow accesss by classes in the same package or by subclasses of that class. Peach isn't in the same package and it doesn't subclass PubClassInOtherPkg so, what you are seeing is a correct exception. If you had public class Peach extends PubClassInOtherPkg Then you could access protI. Access to an instance variable is determined by the class you are in when you attempt to access it. Your attempting to access it from Peach and that class isn't allowed access (even tough you are trying to access it on an Object of PubClassInOtherPkg) ------------------ Hope This Helps Carl Trusiak, SCJP2
diabolical! so it actually matters "where you are" when you try to access protected members... i never picked up on that. i just figured, from "wherever you are", you can make an instance of an object, and then start using the . operator to get at it's members---and access privileges (the modifiers) were purely what determined whether or not you could get at them. i'm going back to do some re-reading. thank you for the important tip carl! of course, i tried this (in a new file, now named ChildOfPubClassInOtherPkg.java---after removing this class's declaration from Peach.java):
and it ran as you said it would. perhaps someone could tell me this: if i buy a software class library from someone, all their classes will be in their (the company i bought it from) own package or packages. now, since i'm writing software, i may wish to use their classes in 2 ways: 1. just declare instances of their classes in my own class's body or methods (and within my own packages) and use as needed, accessing only the public members of the instance. 2. subclass the classes they provide. these subclasses will be classes themselves, each in their own .java files in my own packages. from my experience so far, it sounds like the only way i can now make use of the protected members is actually *within* the subclasses i've created---hmm... is this the way java software engineering works? how *does* it work? (in this regard) any insights would be gladly appreciated! now, for me it's back to the books!
John M. Gabriele
Joined: Feb 18, 2001
got it. in the JLS, section 6.6 and especially section 6.6.7 spells a lot of it out.
subject: this use of protected should be correct, no?