Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is one situation where the java syntax hides what's really going on, and C# makes it a bit clearer.</p> <p>In C# your B would look like</p> <pre><code>class B : A { public B() : base(method()) { } private static String method() { return "method invoker"; } } </code></pre> <p>Although the java syntax places <code>super(method)</code> within the constructor it's not really called there: All the parent initialization is run <strong>before</strong> your subclass constructor. The C# code shows this a little more clearly; by placing <code>super(method())</code> at the first line of the java constructor you're simply telling java to use the parameterized constructor of the super class rather than the parameterless version; this way you can pass variables to the parent constructor and they'll be used in the initialization of the parent level fields <strong>before</strong> your child's constructor code runs. </p> <p>The reason that <code>super(method())</code> is valid (as the first line in a java constructor) is because <code>method()</code> is being loaded with the static elements--before the non-static ones, including the constructors--which allows it to be called not only before <code>B()</code>, but before <code>A(String)</code> as well. By saying</p> <pre><code>public B() { String s = method(); super(s); } </code></pre> <p>you're telling the java compiler to initialize the super object with the default constructor (because the call to <code>super()</code> isn't the first line) and you're ready to initialize the subclass, but the compiler then becomes confused when it sees that you're trying to initialize with <code>super(String)</code> after <code>super()</code> has already run.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload