Think in Java P269
If the inner class in the subclass is the same as the inner class in the parent class, it seems that the subclass inner class "Overrides" The inner class of the parent class, but does not actually represent anything.
Public classBigeggextendsEgg { Public classYolk { Publicyolk () {System.out.println ("Bigegg.yolk ()"); } } Public Static voidMain (string[] args) {NewBigegg (); }}classegg{Privateyolk y; protected classYolk { Publicyolk () {System.out.println ("Egg.yolk ()"); } } PublicEgg () {System.out.println ("New Egg ()"); Y=Newyolk (); }}
Output Result:
New Egg ()
Egg.yolk ()
The result shows that the inner class does not exhibit other characteristics in inheritance, and the two are two completely independent entities.
classEGG2 {protected classYolk { PublicYolk () {print ("Egg2.yolk ()"); } Public voidF () {print ("Egg2.yolk.f ()"));} } PrivateYolk y =Newyolk (); PublicEgg2 () {System.out.println ("New Egg2"); } Public voidInsertyolk (yolk yy) {y =yy;} Public voidg () {y.f ();} Public voidprint (String msg) {System.out.println (msg); }} Public classBigEgg2extendsEGG2 { Public classYolkextendsEgg2.yolk { PublicYolk () {print ("Bigegg2.yolk ()"); } Public voidF () {print ("Bigegg2.yolk.f ()")); } } PublicBigEgg2 () {Insertyolk (Newyolk ()); } Public Static voidMain (string[] args) {Egg2 e2=NewBigEgg2 (); E2.G (); } Public voidprint (String msg) {System.out.println (msg); }}
Output:
Egg2.yolk ()
New EGG2
Egg2.yolk ()
Bigegg2.yolk ()
Bigegg2.yolk.f ()
1. Parent class Variable Initialization
Private yolk y = new yolk ();
The yolk of the parent class is called, so the result of the output is Egg2.yolk ()
2. Parent class constructor, output new EGG2
3. Sub-class constructors
Insertyolk (new yolk ());
Using the yolk of the subclass, instantiating the subclass, you need to instantiate the parent class, so the result of the output is
Egg2.yolk ()
Bigegg2.yolk ()
4. Code E2.G ();
The yolk in the parent class is actually the yolk of the subclass, so the result of the output is Bigegg2.yolk.f ()
Java inner class "Overwrite"