Here's where I'm a little confused: My book suggests starting @ the classes first to check for access and then to the members. So, looking @ these classes I see that they are both default or package private and yet class Moo can access the Zoo class. Is this because Moo actually extends Zoo? and because of this extending does it indicate both classes as being in the same package? It's my understanding that if a class cannot access the other, the members access control is a moot point. Can someone clarify? TIA
You can tell what package a class is in by looking at its first line of code, which says what package it is in. (Unless it's in the default package, the one with no name.)
Paul Clapham wrote:A "package private" class may be accessed by a class in the same package or by a class extending it.
I agree with the first half of that statement. In this example, Moo can access Zoo because they're in the same package. If they were in different packages, Moo wouldn't be able to extend Zoo unless Zoo was made public.
Of course in real life you would just scroll up to the top of the code to look for the package declaration.
Paul Clapham wrote:Sorry... Greg has it right, and I'm wrong. What I described was "protected".
... only there's no such thing as a protected (or private) class, unless you're talking about an inner class. We discussed the reason why not over in this thread: http://www.coderanch.com/t/410134/java/java/private-protected-class-why-not
The clearest explanation was by this guy:
Paul Clapham wrote:
But if we attempt to assign a meaning to the first part [i.e., having a class defined as protected], then there are two possibilities:
(1) Nothing outside the protected class's package can extend it -- this is equivalent to making it package-private.
(2) Anything can extend it -- this is equivalent to making it public.
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS