Re: Constructor Chaining Conundrum



Ahh, the fog has lifted. I added a new method to the Base class: one
that is not overriden by the child. I then have the Base constructor
call it instead.

Base()
{
// print( );
anotherPrint();
}
void anotherPrint( )
{
System.out.println("Super " + s);
}

Now, I'm getting the behavior I was expecting. I'm studying for the
SCJP and this was one of the Whizlabs testing simulator
(www.whizlabs.com) questions. Thanks for your help and those links.
I'll also be using my debugger as a study aid too.

Hendrik Maryns wrote:
Why doesn't this surprise me? Have you ran it through a debugger? You
will see exactly what happens: s in Derived *shadows* s in Base, whereas
print() in Derived *overrides* print() in Base; thus if Base's
constructor calls print(), Derived's print() will be called (we are
creating a Derived, after all, the unnecessary cast doesn't change
anything at that, that's polymorphism at work), which uses its own s,
shadowing Base's s, and at that point, the initialisation of s, which
happens *after* calling super(), has not yet been done, thus Derived.s
is null, and you get Sub null in the console.

This is all like it should be. Golden rule: do not call methods in a
constructor which can be overridden in a subclass.

Also see http://mindprod.com/jgloss/constructor.html#INITIALISATION and
http://mindprod.com/jgloss/shadow.html

H.

.



Relevant Pages

  • Re: Do some computation before calling super constructor.
    ... Imagine that a subclass overrides a ... method used in the constructor. ... fields in the subclass that have not yet been initialized. ...
    (comp.lang.java.help)
  • Re: Can a classs constructor call another constructor "later"?
    ... I find the current constructor behaviour ... with "constructor overrides behave in the opposite way to method ... choose to call the base method wherever you like, ... I should point out that what Alex did in his first example ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Extend a class without calling the constructor
    ... I plan to do this by creating a class B which extends A, and overrides the method. ... really have to then make the overriding method correct even before you constructor has executed. ... There are hacks with thread-locals, synchronised statics or inner classes to hand context information over. ...
    (comp.lang.java.programmer)
  • Re: Inheritance Question
    ... overrides the property to set their specific url. ... Public SessionKey As String ... Public Class ServiceTicket:Inherits AuthHeader ... The only time I've used shadows is when I don't control the base class ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Derived class calling overriden base class methods
    ... In this case the base method is guaranteed to do what it has to do in order to maintain the base object in consistent state before the child method can start. ... in OOP it is hardly possible to maintain this feature because actually it is not a feature or mechanism but rather a quite different way of thinking about responsibilities and how a system works. ... the external version overrides child methods. ... class DerivedClass: public BaseInterface { ...
    (comp.object)