This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.