Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't call supertype constructor directly - why not?

I have the following example classes in Java:

public class A { }

public class Super {
    protected Super() { }
    public Super(A a) { }
}

public class Sub extends Super { }

public class Consumer {
    public Consumer() {
        Sub sub = new Sub(new A()); //compiler error
    }
}

The compiler error states that the arguments cannot be applied to the default constructor in Sub, which is perfectly understandable.

What I'm curious about is the rationale behind this decision. Java generates the default empty constructor in Sub; why can't it call it behind the scenes in this case? Is this primarily a case of sane hand-holding, or is there a technical reason?

EDIT

I'm aware that this is a language limitation. I'm curious about why it is a language limitation.

EDIT 2

It seems that, as is often the case, I was too close to the code I was actually working in to see the big picture. I've posted a counter-example in the answers below that shows why this is a Bad Thing®.

like image 844
Matt Mills Avatar asked Oct 25 '11 02:10

Matt Mills


2 Answers

I think it's an issue of both readibility and not assuming intent. You say

Java generates the default empty constructor; why can't it call it behind the scenes in this case?

Yet to me, it would make much more sense for Java to implicitly call the Super(A) constructor "behind the scenes" than to call the Super() constructor, disregarding A.

And there you have it. We already have two disparate assumptions about what should (or could) happen in this case.

One of the Java language's core principles is transparency. As much as possible, the programmer should be able to see by looking at the code what will happen, sometimes at the expense of convenience or magic at the syntax level.

A parallel tenet to that is not assuming intent: in cases where the programmer's intentions seem ambiguous, the Java language will sometimes favour a compile error rather than automatically chosing a default through some (arbitrary or otherwise) selection algorithm.

like image 173
Mark Peters Avatar answered Sep 30 '22 04:09

Mark Peters


public class Sub extends Super { }

does not have the constructor Sub(A a), it only has the default constructor Sub().

Constructors are not inherited.

like image 42
Bhesh Gurung Avatar answered Sep 30 '22 05:09

Bhesh Gurung