File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes public, private, protected, friendly & private protected. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public, private, protected, friendly & private protected." Watch "public, private, protected, friendly & private protected." New topic
Author

public, private, protected, friendly & private protected.

Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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


hello everyone i'm new here.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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 ]

How simple does it have to be???
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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

Joined: Jul 21, 2003
Posts: 522
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.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Feb 28, 2004
Posts: 27
what is API? where can i check that?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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
 
GeeCON Prague 2014
 
subject: public, private, protected, friendly & private protected.