Inheriting nested classes to a subclass

When I went through this article in the Private Members section of the superclass , I saw this line

A nested class has access to all private members of its class application - both fields and methods. Consequently, an open or protected nested class inherited by a subclass has indirect access to all private members of the superclass.

My question is: how can we directly access the Nested Base class in Derived (for example, we can access any public , protected fields)?

and

if there is a way how Derived access p , which is a private field from Base to 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' ? } } 

Thank you in advance for your time and efforts in this topic!

+4
source share
3 answers
 Base.Nested theClassBro= new Base.Nested(); 

Or for the Derived class, this should work:

 Derived.Nested theClassBro= new Derived.Nested(); 

I'm not sure if you need to use the super keyword or not

+3
source

A non-stationary inner class always needs an enclosing instance of the class in which it is nested. Inside the code defined in the Base class or Derived class (since it inherits the inner class), you can simply write

 Nested nested = new Nested(); 

To create a new instance. Then you can call the getP () method on the nested link to get the value of the private value p. This value is part of an instance of the Base class that includes an Nested instance.

Because the inner class is publicly available, code defined outside of Base or Derived can also instantiate. But this requires an attached instance of Derived or Base. Java has special syntax for this, where you invoke a new statement on an instance of the enclosing class. So, outside of Base or Derived, you can do:

 Base base = new Base(); Base.Nested baseNested = base.new Nested(); Derived derived = new Derived(); Derived.Nested derivedNested = derived.new Nested(); Base.Nested d = derivedNested; 

You can also import Base.Nested so you can write:

 Base base = new Base(); Nested baseNested = base.new Nested(); Derived derived = new Derived(); Nested derivedNested = derived.new Nested(); // Base.Nested reference 

It's good to know this syntax, but I feel that the code is generally cleaner (easier to understand, better encapsulation), if only the closing class is allowed to create new instances of the inner class. You can also use a static nested class if you need a class that logically belongs only to Base but does not need a closing instance.

+1
source

As you know, Nested can only be created when there is an instance of the class containing the definition of the Nested class, in our case it is the Enclosing class. To access the private members of the Enclosing class through the inheritance of the Nested class, we need to provide the Derived class constructor that spans the instance containing Enclosing.Nested . The following code should be clearer. I changed the names of the variables and classes from your original example for a better understanding:


 public class Enclosing { protected int protectedMember = 3; private int privateMember = 7; public class Nested { public int getPrivate() { return privateMember; } public int getProtected() { return protectedMember; } } } 

 class Derived extends Enclosing.Nested { //Provide the enclosing instance that contains Enclosing.Nested public Derived() { new Enclosing().super(); } //Access protected member of Enclosing class public void accessProtectedMember() { System.out.println(getProtected()); } //Access private Member of Enclosing class public void accessPrivateMember() { System.out.println(getPrivate()); } } 

 public class Test { public static void main(String... args) { Derived derived = new Derived(); derived.accessProtectedMember(); derived.accessPrivateMember(); } } 
0
source

Source: https://habr.com/ru/post/1491948/


All Articles