3

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?

GameDroids
  • 5,584
  • 6
  • 40
  • 59
Abhishek Keshri
  • 3,074
  • 14
  • 31
  • Well... Encapsulation for which class? For the outer class, it's encapsulated. – ernest_k Aug 30 '18 at 12:52
  • We are directly getting the value of `x`. How is it encapsulated for outer class? – Abhishek Keshri Aug 30 '18 at 12:55
  • 1
    Your example shows the technical possibility of accessing inner classes and their attributes from an outer scope, but if it can be regarded as bad style really depends on your requirements and coding guidelines you're following. Make the inner class private maybe, and/or its members and methods. – deHaar Aug 30 '18 at 12:57
  • Same question here: https://stackoverflow.com/questions/17881464/java-inner-class-and-visibility-of-private-fields – jaco0646 Aug 30 '18 at 16:32

2 Answers2

3

The private access modifier is used so that we can use the respective member only within the class.

Since the inner class is part of the class, this still holds. The access to private data is confined within, so encapsulation is preserved. Besides, since you're able to modify the source file of the class, it means you have access to all its internals anyway.

memo
  • 1,868
  • 11
  • 19
  • but we are able to access the private member out of the class, doesn't it defy what encapsulation intends to enforce? Shouldn't java enforce a check to ensure that? – Abhishek Keshri Aug 31 '18 at 05:14
  • It's not out of the class since it's from an inner class! You don't break encapsulation by accessing private class fields from the class' own methods, do you? – memo Aug 31 '18 at 05:24
  • Yeah I think I get what you mean, encapsulation ensures the data hiding for that class. We can get that data by other ways (using a getter, using inner classes etc.) as per the needs of the programmer. – Abhishek Keshri Aug 31 '18 at 05:26
1

The "Inner" class is a part of the "capsule" - the Outer class. So it is absolutely ok, that it can access private variables of the Outer class. The point of Encapsulation is to hide parts of implementation from the outside and the "Inner" class is not outside, it is inside the class.

Igor O.
  • 193
  • 1
  • 6
  • RishikeshDhokare, "A nested class is a member of its enclosing class." https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – Igor O. Aug 30 '18 at 13:43
  • Ideone appears to be a demo class that is accessing a nested class. – Howard P Aug 30 '18 at 13:49
  • 1
    RishikeshDhokare, I wrote about the "Inner" and the "Outer" class. "Ideone" is not accessing the private variable of "Outer", it is the "Inner" class, that is accessing it. If you want to achieve, that "Ideone" has no access at the "getX()"-method of the inner class, you have to declare the method as private. – Igor O. Aug 30 '18 at 13:52
  • @IgorO. but using the inner class, we are able to access the private members of outer class – Abhishek Keshri Aug 31 '18 at 05:15
  • Abhishek Keshri, as mentioned in my and other answers, it does not violate the rules of encapsulation, since the inner class is in the class, whose private variables it is accessing. – Igor O. Aug 31 '18 at 06:52