Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classes

 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class OuterOne{
private int x = 0;
protected class InnerOne{
void amethod(){
System.out.println("xyz");
}
}
}

public class InSomeOtherClass{
public static void main(String[] args){
OuterOne.InnerOne io = new OuterOne().new InnerOne();
io.amethod();
}
}
how can InSomeOtherClass has access to protected member class(Inner class)InnerOne.?
according to encapsulation rules, protected member variables or inner classes are accessible only to the derived classes and cannot be accessed outside the class.
can anybody pl. explain me about the above problem
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These classes are all in the same package by virtue of having no explicit declared package. No access rules have been violated.
 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i.e we can access the protected variables, if the related classes are in the same package.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic