Explain who can, how the inheritance of nested non-static classes occurs. An example of the following code:

public class Auto { public class Door { private double height; public Door(double height) { this.height = height; } } } public class LadaDoor extends Auto.Door { public LadaDoor(Auto auto) { auto.super(0); } } 

Why the designer looks that way. Why does the call to super() on the outer class object call the inner constructor ??

    1 answer 1

    An instance of a non-static inner class is always associated with an outer instance. In your case, Door cannot exist without any particular Auto instance.

    Therefore, when creating an instance of a child class with respect to the internal one, it is necessary to associate it with the object that will own it.

    The expression auto.super(0) denotes a call to the parent class constructor ( Auto.Door ) in the context of the auto instance.

    In the terminology of the Java language, this is called the Qualified Superclass Constructor Invocation (concretized call to the parent class).

    Qualified superclass constructor invocations begin with a Primary expression or an ExpressionName. It is a constructivist to explicitly direct superclass (§8.1.3). This may be necessary when the superclass is an inner class.

    Adapted translation:

    The concretized call to the parent class constructor begins with an expression. It allows the constructor of the child class to explicitly specify the instance that will contain the object being created. This may be necessary when the parent class is internal.

    • auto.super (0) means calling the parent class constructor (Auto.Door) in the context of the auto instance , this moment is not very clear, what does calling the superclass in the context of any object? - Denis
    • Well, look. An object of the LadaDoor class cannot exist by itself (it is inherited from the inner class). It must be "attached" to some instance of the class Auto . This form of recording allows the user to tell which Auto object will store our LadaDoor object. - Nofate
    • The LadaDoor constructor could have no parameters at all, for example, but to call the parent constructor like this: (new Auto()).super() or MyAutoBuilder.getAuto().super() (there is no talk of the usefulness of this code snippet). - Nofate
    • In your case, an instance of Auto is passed to the LadaDoor constructor, and we make it the "owner" of the object being created. - Nofate