• 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

Thoughts on Oddity: Objects of same class able to access private members directly

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an object of a particular class, say class A, recieves an object of the same class as a parameter to one of its methods, how come the method is able to access the private members of the parameter object directly. E.g.,
class A{
int a;

void f(A a){
this.a = this.a + a.a;
}
}

Dont you consider this behaviour odd? I have tried and can verify that this works in Java; its just that i didn't expect this work. i had expected to use a get method.

what your thoughts on this? any other oddities/unexpected behaviours that you noticed?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everybody considers this odd. They think "accessible in the same class" means "accessible in the same object." It doesn't; it means "accessible from any object of the same class."
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Encapsulation generally means that a class does not expose its implementation and internal members to other classes, so that the implementation can change not affecting the clients. However, being inside the class A, you are aware of the implementation and internals, so you can access them safely. Of course, from my experience, it's better to use the public API if it's possible as calling non-private methods allows overriding in subclasses. And it's easier do break the invariants/contracts when using the private members.The same would apply to accessing the private fields vs. public getters/setters - if, as in our example, the parameter a of the method f() is of a subclass of class A, calling a.a will always return statically the field from the class A. But calling getA()/setA() can be overridden if the a parameter is of a subclass of A and can do something else or something more than just setting the value of a.a. Then it depends, what you want to allow and achieve...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic