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 ...

I think you mean class B and class A. If B extends A, then an instance of B is itself an instance of A.


( 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. )

And abort(?) if the method is abstract.

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?

The idea of overriding is that the base classes method is overridden always. It does cause problems in that you can have a method called which tries to use fields that have not been initialised yet, but it's better to fail obviously in development than do something subtly wrong through to production.

This means that factory methods work from the constructor. For instance you'll often see in Swing components have protected create methods that are called from the constructor.

If you really wanted to, you could add an "initialised" boolean variable. Set it after the super constructor is called. In you overriding methods, if it has not been set just call the super method and exit. I'd be interested to see any uses of this, as in nine years of Java programming I have not come across one.

Tom Hawtin
.



Relevant Pages

  • Re: overides/loads usage
    ... #2 But why not always use overloading or shadowing rather than overriding? ... shadowing especially since to override requires the ... procedure in the derived class shadowing or overriding the new procedure ... will fire and then the constructor from class "B". ...
    (microsoft.public.dotnet.languages.vb)
  • ArrayList(ICollection) constructor & overriden ArrayList.AddRange().
    ... I'm encountering a very simple issue with ArrayList constructor ... and AddRange() method overriding. ... constructors versus virtual method calls usage? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overriding constructors
    ... You should be able to write a constructor for your class and explicitly call ... the base implementation, effectively overriding it. ... You can also overload ... I do not necessarily consider this to be overriding so much, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overloading ctor doesnt work?
    ... > datetime is immutable so overriding the constructor doesn't change the ... Ahhh! ... Thanks a bunch, now this makes things much clearer. ...
    (comp.lang.python)
  • 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)