Objective
Because the constructor of the inner class must be connected to a reference to its perimeter class object, things can get a little complicated when inheriting the inner class. The problem is that the "secret" reference to the perimeter class object must be initialized, and there is no longer a concatenated default object in the exported class. To solve this problem, you must use special syntax to clearly clarify the relationship between them:
Sample source Code
Package Com.mufeng.thetenthchapter;class Withinner {public Withinner () {//TODO auto-generated constructor StubSystem.out.println ("Withinner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Withinner";} Class Inner {public Inner () {//TODO auto-generated constructor StubSystem.out.println ("Inner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Inner";}}} public class Inheritinner extends Withinner.inner {public Inheritinner (Withinner WI) {//TODO auto-generated constructor s Tubwi.super (); System.out.println ("Inheritinner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Inheritinner";} public static void Main (string[] args) {Withinner wi = new Withinner (); Inheritinner II = new Inheritinner (WI); SYSTEM.OUT.PRINTLN (ii);}}
Result output
Withinner Constructorinner Constructorinheritinner Constructorinheritinner
SOURCE parsing
can see thatInheritinnerInherit from the inner class only, not the perimeter class. However, when you want to build a constructor, the default constructor is not good, and you cannot just pass a reference to a perimeter class object. In addition, the following syntax must be used inside the constructor:
Enclosingclassreference.super ()
This provides the necessary references before the program can be compiled.
Inheritance of classes within Java (with source code)