Re: Rationale behind constructor call chain... ( and comparison with C++)
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Oct 2006 21:53:38 -0500
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
.
- References:
- Prev by Date: Re: Why Java's math expression (power) is so inconvenient and error prone?
- Next by Date: Re: How to show the table and not the string in struts
- Previous by thread: Re: Rationale behind constructor call chain... ( and comparison with C++)
- Next by thread: How to make display sleep (aka go off) in Java cell phone program
- Index(es):
Relevant Pages
|