File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Accessibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Accessibility" Watch "Object Accessibility" New topic

Object Accessibility

Pawan Kumar

Joined: Sep 04, 2000
Posts: 23
x is a object ,y is also a object of same class ,can x access the provate data of y,this question was on javaranch mock per me it's wrong ,beacuse that the rule of OOP's ,data encapsulation.can anyone help?
mahesh deshpande

Joined: Oct 26, 2000
Posts: 19
Dear Pawan
It is possible for object to access the private data of another object of the same class.
kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
Can anyone tell me how this is possible??

Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Think private static. One class but all objects can reference it and change it.
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
This is correct because the access modifiers restrict access based on the class type and not specific instances of that class. Although x and y are different instances, they belong to the same class and therefore y can access the private data of x.
Gopala Krishnan

Joined: Mar 04, 2001
Posts: 6
guys every object makes it own copy of the instance variables and methods(excluding the static variables and methods), so one object cant access the copy of the instance variable of the other object, eventhough they are from same class.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
Private means private to the class and not to the object.
Pawan Kumar

Joined: Sep 04, 2000
Posts: 23
hi everybody.
i still not able to get it.x and y are two instance of a same class.they have their private data with them.and thats the data encapsulation.only the method's of that object can access the data of that object.same is the case with other object.
here we are not considering that private data is static.if it is then it ,then that data belong to the class not to a specific object.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
class Complex {
private double real,imaginary;

public Complex(double r,double i) { real = r; imaginary = i; }

public Complex add(Complex c) {

return new Complex(real + c.real,imaginary + c.imaginary);
class Client {

void useThem() {
Complex c1 = new Complex(1,2);
Complex c2 = new Complex(3,4);
Complex c3 = c1.add(c2);

on line 16 a call is made to c1.add(c2). Object c1 will execute the method,using object c2 as a parameter. In line 7,c1 accesses its own private variables as well as those of c2.There is nothing wrong with this.Declaring real and imaginary to be private means that they may only be accessed by instances of the Complex class,but they may only be accessed by any instance of Complex.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
I had to delete my earlier post about not being able to come up with an example to test the point in question, thanks Shabbir for making me feel like a total dolt.
Just kidding
I appreciate your taking the time to enlighten me, thanks.
~ Ryan Burgdorfer
Java Acolyte in
Columbus, OH USA

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
I agree. Here's the link:
subject: Object Accessibility
It's not a secret anymore!