if you are defining any private members , then those members can be accessed in the class only , then how can we access private members of the inner class in the outer class
I hope you know that Inner classes (regular inner classes --not method local) can access private members of enclosing class , as like other class members --- did you give it a thought why so..?
Reason is -- Inner class is treated as a member of outer class --similar to methods and variables of a class. So Inner class can access private members of enclosing class and enclosing class can access private members of inner class..
Hi, Remember Innerclass can access private members of outer class and Outer class can access private members of innerclass provided in both cases the private members are being accessed within the class. Inner classes are treated as members of outer class which means the private[or any members] members of inner class are treated by its enclosing class as if they were members of its own class. So as you know in a normal case any private member can be accessed only within the class , The same concept can be applied to inner and outer class memebers.
Innerclass can access private members of outer class via an implicit reference of outer class. This implict reference of outer class can be accessed in your inner class through "MyOuter.this".
Outer class can access private members of innerclass via an instance of the inner class. Check out the below code:
I hope this clears your confusion. Suggestion : Use better names for your class , for example your inner class is called LimeTree and your outer class is called Inner4. It can cause confutions when your trying to learn the concept. Thanks Deepak
More generally, the private members of an inner class can be accessed anywhere else within that top-level class--including by another inner class that has no inheritance or container relationship with the first inner class.
I posted some sample code illustrating this in another thread from last week. Hope this helps!