The private
access modifier is used so that we can use the respective member only within the class. But using inner classes, we can define a method to access the private
members of the outer class. Here is the code for that:
import java.util.*;
import java.lang.*;
import java.io.*;
class Outer {
private int x = 1;
Inner getInner() {
Inner inner = new Inner();
return inner;
}
class Inner {
int getX() {
return x;
}
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Outer outer = new Outer();
Outer.Inner inner = outer.getInner();
System.out.println("Private x: "+inner.getX());
}
}
Doesn't it go against the concept of Encapsulation?