• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public/private code

 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peace & Greetings to all....

I have been studying Java for about 2 weeks and I have to say that being a network guy, I love it so far. I am currentyl reading "Java 2: A Beginner's Guide" (Herbert Schildt). However, under the section "Encapsulation", the author is explaining in terms of how within an object, code or data (or both) may be 'private' or 'public' to the object. The author also mentions that private code cannot be accessed by a piece of the program that exists outside of the object.

My question is, what is meant by a piece of the program existing 'outside' of the object? He sort of lost me on that statement.

Thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

Objects talk to each other all the time. Something as simple as

has the object holding this code referencing System's public variable called out, which happens to be an output stream of some kind. Then we talk to out and call its public println() method. So your code is "outside" of System, and whatever stream object that out refers to.

Right now, go to the JavaDoc for System to see how it documents its public out variable. Then follow the link to the type for out and see its public println() method.

System probably does a lot of other stuff that it keeps private. We don't even see the private methods and fields in the doc.

Does that make sense?
[ August 11, 2006: Message edited by: Stan James ]
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Stan.....

It sort of makes sense, but i'm still not grasping the concept fully. I'll keep at it until it clicks.


Henry.....

Sorry about not following the guidelines.
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Derek Harper:
Thanks for your reply Stan.....

It sort of makes sense, but i'm still not grasping the concept fully. I'll keep at it until it clicks.


Well, let's look at some more code...



In this case, we have a class that has two methods, one public and one private... Now you want to call the methods of this class. Say you have this code.



The call to myMethod1() will definitely work -- that method is public and can be called by anyone. The call to myMethod2() will depend on where the calling code is.

If this code is in a method of the MyClass class, then it will work. Otherwise, it will fail to compile because the myMethod2() is private and can't be called by code "outside" of the MyClass class.

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Harpo:
The author also mentions that private code cannot be accessed by a piece of the program that exists outside of the object.


This is not fully correct - other objects of the same class can access it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classes and objects have a "public" face and a "private" heart.
The "public" face, which means those bits declared as "public" (there are other forms of access too), is available to any Java programming which happens to get its hands on your class. The remainder is kept hidden from the outside world (though as Ilja Preuss told you, private members of an object are accessible to other objects from the same class).
If you look at the javadoc for any class in the API, you see its "public" face (and you may see some non-private bits too).
Look at this little class:- You will have to produce a Person class and a BankAccountTest class to use this class, but notice:-

[list]You don't have direct access to the "balance" or "holder" fields; if you want to change the holder's address you would have to write[/list]
  • You don't have access to the setBalance method. Try invoking the method as suggested in the Javadoc, and your compiler will refuse.
  • Try compiling the javadoc (use the command "javadoc BankAccount.java" on command prompt).

  • You won't even see the three private members.
  • The details about how the methods work are kept hidden. Nobody needs to know how you calculate the balance after a withdrawal, they just need to know it works.
  • If you change the body of any of your methods, or change a variable so "balance" is changed to "savingsAmount," nobody else will notice any difference.

  • I hope that has helped you. CR
     
    Derek Harper
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks guys....I think it's starting to sink in.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49382
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it's starting to sink in.
    . . . and we are glad to have been able to help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic