File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes this use of protected should be correct, no? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this use of protected should be correct, no?" Watch "this use of protected should be correct, no?" New topic

this use of protected should be correct, no?

John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
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 and in it is:

over in com/thispackage i've got a file called 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: protI has protected access in
could someone please enlighten me? thanks.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
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

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
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 removing this
class's declaration from

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
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
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
got it.
in the JLS, section 6.6
and especially section 6.6.7 spells a lot of it out.
I agree. Here's the link:
subject: this use of protected should be correct, no?
jQuery in Action, 3rd edition