So you all say it's a safety thing, right? It forces a design where
initialization of an object gurantees that all inherited initializations
will execute first.
Nice.
But many other languages allow you to override constructors! Are they
old, obsolete?
Appealing to "other languages" isn't of much use. Many
other languages, for example, have `goto'.
Are there any other reasons besides good design?
If we talk about errors that may occur if you could override a
constructor then we could say those same erros could occur if you
override any other protected method.
Since Java cannot prevent all errors, is it your belief that
it should not bother trying to prevent any at all?
As to the "any other protected method" remark, note that a
constructor is not a method despite many surface similarities.
Why is proper construction sequence so important? Why not just leave it
to access modifiers? Wouldn't that be more straightforward than having
silly rules like "super() must be the first line"??
1: So a class can establish its invariants, and not worry
about a subclass messing them up. (1a: So a subclass can rely
on its superclass to operate as advertised, without the subclass
needing to take on the responsibility for correct operation.)
2: I don't understand the suggestion. What does access have
to do with sequencing? Accessibility is a static attribute, not
a time-varying property. (Absent skulduggery, that is.)
3: Since I don't understand (2), I can't tell whether it
would be less silly.
I mean, if it's a call, and not a syntax issue I'd like to write it
wherever I like. Let _me_ worry about proper initialization.
No, because I don't trust you. I've written my class, and
you're extending it, and I have no idea whether you know enough
about my class' internals to be able to duplicate them in your
code. (In fact, I've used an obfuscator on my class' byte code
specifically to conceal those internals from you. If you know
enough about them to replace bits and pieces at will, you must
have worked hard to reverse-engineer the code in violation of
your license, and my attorneys will be in touch with you.)
Even without the licensing and trade secrets stuff, think
of the position you're putting the superclass' author in if you
allow any old subclass to muck with the internals. Some user
reports that there's a bug in my class; I say "Thanks for the
report" and send a fix. The fix involves adding a new private
reference variable, and having the constructor initialize it to
point to something useful. Other methods then use that variable
to get at the useful goodies, and my fix solves the problem.
... except that you have somehow overridden my constructor
(you clever devil), so my constructor's code never runs and the
added reference remains null. When another method comes along,
there's a NullPointerException and I get another bug report from
a user who's becoming very angry at my inability to fix things.
I guess from your perspective this is a Good Thing: You've made
the mistake of failing to read my mind and anticipate all my
fixes, but I take the heat for your failure!
No, thanks, Pitch. I'm too egotistical about my own code
to allow you to inject yours into the middle of it and then get
me to take the blame.