In the code snippet shown below, an inner class inherits an outer class itself.
package test;
class TestInnerClass {
private String value;
public
The other answers have explained well why you are getting the results you see (you have two instances of TestInnerClass
and are accessing the first), however there is actually a way to access the private members of TestInnerClass
in its subclass - the keyword super
.
If you replace your showValue
method with this:
public void showValue() {
System.out.println(super.getValue());
System.out.println(super.value);
}
You will get the output that you expected.
I would also suggest that if you decide to do this you make InnerClass
a static inner class because it no longer needs a reference to an instance of its outer class.