Re: Rationale behind constructor call chain... ( and comparison with C++)



A.B. wrote:

Just a question...

If an object B extends an object A, whenever B is constructed, A is
constructed... if A() calls a
function that is overloaded in B, it looks like the overloaded function
is called by the A constructor ...

No, that never happens. A's constructor never calls an
overloaded method of class B.

Of course, I'm mocking you a little bit here. A's constructor
might call a method that is over*ridden* by class B, if the coders
of A and/or B were careless. But please: If you're going to debate
fine points of the language, proper use of the existing nomenclature
will be an aid to fruitful discussion.

( In C++ the behavior is always to call the function defined in A when
called within the A constructor, even if the overloaded function is B
is virtual. )

Can't comment; have avoided C++ with the same unreasoning
hatred that kept me away from COBOL.

Can someone explain me the rationale behind java's behavior in that
case, how it's useful, more logical etc than the C++ approach... can
this behavior be overriden?

At a guess, the rationale is simplicity, aka uniformity. As
things stand, the rules for routing method calls are the same within
constructors as they are everywhere else, and special cases are not
required. If I understand your description of C++ behavior correctly,
it seems there must be a "constructor mode" during which the rules
behave differently. Further, it seems this mode must actually be
dynamic: if method M1 calls method M2 but M1 can be called both
from a constructor and from another post-construction method, is
it supposed to choose A's native M2 during A construction and B's
overriding M2 thereafter? Ay-ay-ay!... If that's how C++ actually
behaves, I think I've been right to avoid it.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: enforce override of method
    ... public void foo() ... abstract class B_Base extends A { ... same package) a protected or public one. ... And of course anyone who mentions that A's constructor could have code ...
    (comp.lang.java.programmer)
  • Re: Extending the mysqli class
    ... Under PHP 4: ... print "Constructor called\n"; ... // it extends A. ... There is no clue within ...
    (comp.lang.php)
  • Re: __CLASS__ Not what expected :/
    ... > class two extends one { ... What you're doing here is potentially very dangerous: you are calling the ... constructor of the class 'one' from it's subclass. ... function echoClass() { ...
    (alt.php)
  • Re: dont understand constructor...
    ... class b extends a{ ... The Java compiler inserts a call "super" to the superclass constructor at the start of a subclass constructor unless you do so explicitly yourself. ... One question mark is enough. ... Your degree of puzzlement does not affect the answer you get and anyway, there is no internationally calibrated system for assigning a number of question marks to degree of puzzlement. ...
    (comp.lang.java.programmer)
  • Re: If you were inventing CoBOL...
    ... in order to fit the OO idiom into Cobol it uses ... 'New' is _not_ the constructor, ... it is (in Cobol) the name of a method. ... J4 have to navigate a path between 'Cobol roots' and 'OO idiom' for ...
    (comp.lang.cobol)