Compiler calls to no-arg super() in argumented constructors of child classes in java -
here goes:
public class parent { public parent(string name) { this.name = name; } public string name = null; } public class child extends parent { public child(string name) { super(name); // if comment : implicit super constructor parent() undefined. must explicitly invoke constructor } }
it said "if don't invoke super constructor self, compiler insert no-arg call super() you, first statement in constructor."
- is true argument-ed constructors well?
- mustn't compiler call argument-ed super constructor same signature of child constructor?
- why has been designed this?
is true argument-ed constructors well?
yes.
mustn't compiler call argument-ed super constructor same signature of child constructor?
no. if want different base-class constructor invoked, must call yourself, passing arguments necessary.
why has been designed this?
because alternative you're proposing (automatically forwarding arguments) wouldn't useful in general.
Comments
Post a Comment