2

I'm a little bit confused by this code:

class A{
    class B{
    }
}

class C extends A.B{
    C(A a){
        a.super();
    }
}

What does it mean: "a.super();" ? Before i thought super - link to her parent class, but "super()" - link to parent's constructor, but really class A haven't a parent class(don't mind Object...), so what does it mean super in this context?

Thx everybody.

Crank Sworl
  • 21
  • 1
  • 3

2 Answers2

5

super() calls the default constructor of the super class. If you don't define a constructor your class always has a invisible default constructor, which doesn't require any parameters. You are calling the constructor of the Object class in this case.

ssindelar
  • 2,833
  • 1
  • 17
  • 36
  • No, it's wrong. I thought the same. But, actually `a.super()` = `A.B()`, so B's class constructor. I found it here: http://stackoverflow.com/a/2831567/2572927 And i found one interest moment: `class A{ class B{ B(String s){print(s);} } } class C extends A.B{ C(A a){ a.super("C constructor!"); } }` It's normal work, so 'a.super()' = 'A.B()'. I think so, i hope it's truth=) – Crank Sworl Jul 11 '13 at 16:28
3

It is qualified superclass constructor invocation.

From the JLS 1.8

Explicit constructor invocation statements can be divided into two kinds:

  1. Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.

  2. Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass. Superclass constructor invocations may be further subdivided:

  3. Unqualified superclass constructor invocations begin with the keyword super (possibly prefaced with explicit type arguments).

  4. Qualified superclass constructor invocations begin with a Primary expression . They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (§8.1.3). This may be necessary when the superclass is an inner class.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164