The answer is 22 because Extension's version of add is called each time. So i is initialised to 0. new Extension() makes a default call to the super's constructor which contains the line add(1) which calls Extension's add so i = 0 + (2*1) = 2 Then Extension's constructor makes a call to add(2) as well so i = 2 + (2*2) = 6 Then within main, there's the call add(8) so i = 6 + (2*8) = 22 It may seem strange that even the line add(1) in Base's constructor causes Extension's add method to be invoked. But at runtime, the JVM uses the actual class of the object itself to determine which method to call so it works out that b is actually an instance of the Extension class and calls Extension's add method every time. Took me a while to work that one out! Kathy
Above code steps of execution:- 1.bogo with Base b = new Extension() is called. 2.Object type is Extension and reference type is Base. 3.Extension extends base so no arg constr of base is called. i becomes 0. 4.This in turns calls the add(1). 5.Since the object type is Extension the add(1) is the add method of Extension class. 6.This makes v=1 and i =2; 7.Now comes the turn for Extension Constr. This calls add(2). 8.This makes v=2 and I=2+2*2, which is 6. 9.Now b.add(8) is called., still the object type is Extension as the statement Base b = new Extension() is still true. 10.This makes v=8 and i=6+8*2, which is 22. 11.Now the print methos is called, as there is no implementation of print inside the Extension class thus the parent defination is used from the Base class which prints the value of i as 22
SCJP2 2001 84%<br />SCJD2 2003 100%
Joined: Nov 20, 2000
Also if u take out the add method from the extension class then the add method from the Base class is used. This gives a value of 11.
Hi, This topic and the answers really startled me, since i assumed that you have to use super() in the the subclass constructor if you have to invoke the base class constructor. Is that not so? By what you guys have discussed i think you make the point that the super class constructor is implicitly invoked. I would be happy if you clarify since this is significantly different from what i assumed. Thanks in advance, Shree
Hi Shree, When a new instance is created the JVM always creates instances of all it's superclasses; it does so by implicitly calling their no-arg ctors.
If you try compiling the above you receive a compile error. The compiler will automatically create a no-arg ctor for 'B' as none have been declared. It does not do the same for 'A' as a ctor is declared. When we create a new instance of 'B', the compiler automatically looks for the 'A' no-arg ctor and doesn't find one .. result: compile error. However, if you add a ctor to 'B' such as:
The code will compile and run. The compiler will not look for a no-arg ctor in 'A' as we've explicitly told it to use the declared ctor by calling 'super'. Hope that helps. The full set of initialization rules are explained in JLS§12.5. ------------------ Jane The cure for boredom is curiosity. There is no cure for curiosity. -- Dorothy Parker