• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object Accessibility

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 test.as per me it's wrong ,beacuse that the rule of OOP's ,data encapsulation.can anyone help?
thanks
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Pawan
It is possible for object to access the private data of another object of the same class.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how this is possible??
Thanks,
Kapil
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Think private static. One class but all objects can reference it and change it.
Regards,
Manfred.
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
Private means private to the class and not to the object.
 
Pawan Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
pawan
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic