• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help understanding access control  RSS feed

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not getting enough clarity on this example in a book:



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
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "package private" class may be accessed by a class in the same package or by a class extending it. Doesn't your book say that?

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.)
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I think I got it..except in this example, there is no import statement or packages declared! It's only a segment of code and how would you know they are in the same package? Sorry if that's a stupid question, but I've got to get this concept of access control down pat. And no, the book I have only states that control depends on whether the classes involved are in the same package or different packages..that's it. Believe me, I wouldn't have asked if I had read it! My book is more of an intermediate level read. I had thought I had most of the basics, but apparently not?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... Greg has it right, and I'm wrong. What I described was "protected".

Anyway, so Moo has access to Zoo if they are in the same package. But I don't see anything in your code to suggest that they are in different packages. (You didn't post the package declarations.)
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Paul! And this is verbatim from the book and what concerns me...I am trying to understand how to determine this if it is presented this way on the 310-065 exam that I've got to take in the near future. With no packages referenced in the code, how am I to know if they are or aren't in the same pkg? haha
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're taking some kind of test, they aren't going to leave out critical information. So if you don't see a package declaration, you have to assume there is no package declaration. And therefore the code is in the default package. Not unless one of the possible answers is "I can't tell because I don't know if the code fragments are in a package or not", anyway.

Of course in real life you would just scroll up to the top of the code to look for the package declaration.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After completing more exercises in the book, they have commented out where the package contains both classes. That is, on their other examples.
Would be nice to choose that answer on a test though or better yet supply my own choice words

Muchas Gracias to you both....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!