Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can I have access to the objects private property from another object?  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My java skills have gotten a little rusty... could anyone please tell me why I can access the property "text" which is marked private from within the equals method? If the object "other" is a different object, shouldn't I be using an accessor method to access it's private properties?



Thanks.
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The accessibility options like "private" and "protected" control access at the class level, not at the object level.
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response. So if I have a component object of the same class as the the main class, that means that from the main class I can have access to all the component object's properties even if they are private. Right?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Serna wrote:Thank you for the response. So if I have a component object of the same class as the the main class, that means that from the main class I can have access to all the component object's properties even if they are private. Right?



Well, you already have an answer that you apparently think tells you that. Why not write some code and try it? How much confirmation do you need?
 
Marshal
Posts: 58345
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are inside the same class, so yes.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access specifiers actually work with respect to classes.
So if we assign something private, it means that only within that class that member can be accessed and outside it, not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!