This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another question" Watch "Another question" New topic
Author

Another question

ritu mengi
Greenhorn

Joined: Oct 04, 2000
Posts: 15
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

Joined: Aug 04, 2000
Posts: 103
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

Joined: Nov 20, 2000
Posts: 51
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%
Raj Mehra
Ranch Hand

Joined: Nov 20, 2000
Posts: 51
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

Joined: Nov 04, 2000
Posts: 135
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

Joined: Sep 18, 2000
Posts: 208
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


Regards,<BR>Shree
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
jQuery in Action, 2nd edition
 
subject: Another question
 
Similar Threads
Constructors & methods
constructor invocation-doubt
program output
Constructor Problem
How to read this program?