If I have a parent class with a constructor that takes a parameter and I create a child class that extends the parent I cannot instantiate the child by using the parent's parameterized constructor. I must create a constructor with the same signature and call super().
While I can accept thsi behavior as fact, I cannot understand the rationale for such a policy within the language. Would someone care to expound on the philosophy on why this behavior is part of the language?
Joined: Sep 16, 2005
My belief is that the fewer implicit things that happen in a programming language the better. For example, I'd prefer there to be an explicit "package" level access, rather than it being the so-called default level:
Anyhowdy, if you derive from a class, you must explicitly provide constructors, rather than have them automatically generated:
I can think of several reasons for why this is good language design, for instance, my preference for avoiding implicitly generation.
Another reason why is the following. Suppose the rule was that if you provided *no* constructors, Java would try to generate the obvious constructors based on the base class. That may be okay today, but suppose tomorrow, a new constructor was aded to A, one that you didn't want B to have, yet by the above rule, it would sneak into your code.
If you find typing in the matching constructors in subclasses tedious, realize that most IDEs will slap the code down for you in a twinkle.
There is no emoticon for what I am feeling!
Joined: Sep 28, 2005
First of all, constructors are not members of classes, and only memebers are inherited.
Second, we can imagine cases for which we don't want subclasses to have the same constructors than the parent class.
Imagine an abstract class Vehicle with a constructor Vehicle(int wheels), and a subclass Bicycle.
By definition, a Bicycle has 2 wheels so we can imagine that Bicycle constructor will call super(2) and isn't it better in this case that Bicycle does not expose a constructor Bicycle(int wheels) ?
Regards,<br />Seb<br /> <br />SCJP 1.4
Joined: Sep 16, 2005
> First of all, constructors are not members of classes, and only memebers are inherited.
True, but the OP was looking for a rationale, and this just a technicality.