java - Inheriting Nested classes into Subclass -
when going through this article, under section private members in superclass, saw line
a nested class has access private members of enclosing class—both fields , methods. therefore, public or protected nested class inherited subclass has indirect access of private members of superclass.
my question how can directly access nested
class of base
in derived
(like can access public
, protected
fields)?
and
if there way, how can derived
access p
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); // understand inheriting protected field // how access inherited nested class here? , if accessed how retrieve 'p' ? } }
thanks in advance time , effort in thread!
base.nested theclassbro= new base.nested();
or derived class, should work:
derived.nested theclassbro= new derived.nested();
i'm not sure if need use super keyword or not
Comments
Post a Comment