This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Aug 02, 2006
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.
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.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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.