Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public, private, protected, friendly & private protected.

 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello again as some of you might know my name is Erik. alright can anyone tell me what's the difference between these five access modifier. i understand that they affects the the way the user accesses and uses the method but i don't get the idea. can anyone explain to me how they are different, perhaps show me some example thank you.
1. public
2. private
3. protected
4. friendly
5. private protected
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: public, this access specifier can make any variable, method and class be seen by any other class.
2: private, if you make a variable private, then only an object of that class can access the variable.

3: protected, is used when a subclass wishes to have access to the members of the superclass

4: friendly, is not a java keyword or access specifier.
5: private/protected, i have seen somewhere that sun were thinking about it at some point but decided to do without, but i was not sure where i saw it, i think it is in a thread in javaranch somewhere this week, can't remember, but again not used.
the main access specifiers is private (the most restrictive) default (or you ould have meant this to be friendly??? you don't use any keyword to specify, this is the next restrictive) protected (the second less restrictive) and public ( least restrictive, a publics want to party in everyone elses class).
hope this sheds some light on the access specifiers
Davy
[ March 05, 2004: Message edited by: Davy Kelly ]
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Davy Kelly:
4: friendly, is not a java keyword or access specifier.
[ March 05, 2004: Message edited by: Davy Kelly ][/QB]

Hmmm in my reference book it says "The access modifier for a method can be any of the modifiers: public, private, friendly, protected, private protected or static. Most often, methods are given public access." on chapter2 page61.
If you dont believe here's some info about
the reference book which i borrowed from the libruary:
Book title: Java Programming - Comprehensive
Author: Joyce Farrel
Publisher: Course Technology
ISBN: 0-7600-1070-6
Website: www.course.com
[ March 05, 2004: Message edited by: ayukawa madoka ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayukawa-
There is no friendly access modifier in java, it is a C/C++ keyword. In Java the friendly modifier is equivalent to the default (empty) modifier.
Some books and programmers with a background in C/C++ tend to refer to the default modifier as: friendly, package friendly...etc. Personally I find this information confusing and misleading.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vicken Karaoghlanian:
Ayukawa-
There is no friendly access modifier in java, it is a C/C++ keyword. In Java the friendly modifier is equivalent to the default (empty) modifier.
Some books and programmers with a background in C/C++ tend to refer to the default modifier as: friendly, package friendly...etc. Personally I find this information confusing and misleading.

Even then, friendly isn't a C or C++ keyword. C++ *does* have a friend keyword, but C doesn't.
(Just my 2 cents.)
Layne
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:

Even then, friendly isn't a C or C++ keyword. C++ *does* have a friend keyword, but C doesn't.
(Just my 2 cents.)
Layne

details... details... details
Thanks for the tip.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erik,
Check your API to see if there is a friendly access specifier or keyword.
I checked my 1.2 API, it does not have it, and I checked the 1.4 API online and again it does not have it.
Although what Vicken said is true, some books do tend to write to a C or C++ programmer rather than someone who is programming for the first time.
So this tends to mislead a lot of people.
Davy
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is API? where can i check that?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,
A few things.
First of all, the API documentation isn't the place to look for keywords; it doesn't list keywords, it lists the library classes. To learn about the language itself, there's no substitute for a good book.
Now, as far as good books go: the source the O.P. is describing is apparently a horrifically bad book. There is indeed no "friendly" keyword and there never has been. There was a "private protected" access level in the Java beta releases, but it was removed for the final release -- back in 1995! Furthermore, it seems to imply that static is an access level, an alternative to the other levels, which it's not; it's a different thing altogether, and can be used in combination with the other levels. There are other keywords like "static" that can be combined with access levels, which this book seems to leave out: "final" and "synchronized" are the most common.
If you're honestly using study material that claims there's a "private protected" level, then run, don't walk, to the nearest trash can and pitch that book in. Better yet, find a bonfire. Then head out and buy something a bit more modern.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting - I tried to find some info about this book, but the closest I could find was about this second edition which has completely different authors than the first edition did. Perhaps this indicates that the publisher wasn't thrilled with the first edition, and decided to start anew. Though the fact that they have their Java books listed under Java/J++ may also indicate that the publisher hasn't really been paying attention to the state of Java in this milennium.
Since this is a library bok, I can't really advocate burning it or tearing it into shreds (although that might ultimately be a service to other library patrons). But I fully agree with everything Ernest says above. Bad book - do not try to learn from this. Find something else.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I meant language specification, which is part of the SDK, I just call all of it the API, cause if I don't understand anything i check here first.
But the ranch is great as well.
Davy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic