Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>If all objects directly or indirectly inherit from Object how is it possible to elicit the compiler rejection spoken of? </p> </blockquote> <p>I think the basis is of your misunderstanding is that you are <em>thinking</em> that constructors are inherited. In fact, constructors are NOT inherited in Java. So consider the following example:</p> <pre><code>public class A { public A(int i) { super(); ... } } public class B extends A { public B() { super(); ... } } </code></pre> <p>The class <code>A</code>:</p> <ul> <li>does not inherit any constructors from <code>Object</code>, </li> <li>does not explicitly declare a no-args constructor (i.e. <code>public A() {...}</code>), and</li> <li>does not have a default constructor (since it <em>does</em> declare another constructor).</li> </ul> <p>Hence, it has one and only one constructor: <code>public A(int)</code>.</p> <p>The call to <code>super()</code> in the <code>B</code> class tries to use a non-existent no-args constructor in <code>A</code> and gives a compilation error. To fix this, you either need to change the <code>B</code> constructor to use the <code>A(int)</code> constructor, or declare an explicit no-args constructor in <code>A</code>.</p> <p>(Incidentally, it is not necessary for a constructor to <em>explicitly</em> call a superclass constructor ... as I've done. But a lot of people think it is good style to include an explicit call. If you leave it out, the Java compiler inserts an implicit call to the superclasses no-args constructor ... and that results in a compilation error if the no-args constructor does not exist or is not visible to the subclass.)</p> <blockquote> <p>Does it have to do with the constructor being private?</p> </blockquote> <p>Not directly. However, declaring a constructor private will prevent that constructor being called from a child class.</p>
 

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