Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing private member/method from a diff class.

 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the class defn below..
class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iamprivate = 10; // illegal
a.privateMethod(); // illegal
}
}
'iamprivate' and 'privateMethod' are both declared as private in Alpha. I fail to understand how the 2 stmt in accessMethod are illegal despite the fact that they belong to object 'a'.
TIA.

 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the are private and belong to object a. But a is defined in a different class. That's why they are not accesible.

Bosun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic