When I was going through this article, under the section Private Members in a Superclass, i saw this line
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
My question is how can we DIRECTLY access the Nested
class of Base
in Derived
(like we can access any public
, protected
fields)?
and
if there is a way, how can Derived
access p
which is private field of Base
through Nested
?
public class Base {
protected int f;
private int p;
public class Nested {
public int getP() {
return p;
}
}
}
class Derived extends Base {
public void newMethod() {
System.out.println(f); // i understand inheriting protected field
// how to access the inherited Nested class here? and if accessed how to retrieve 'p' ?
}
}
Thanks in advance for your time and effort in this thread!