Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another question

 
ritu mengi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
The answer is 22. Why?
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj Mehra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj Mehra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a little expriment and replace Base's constructor with

Base() {
this.add(1);
}
I expected that it would call Base's add method but
still got 22
Is there any way to call Base's add method from Base's constructor under this circumstances?.
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic