Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Problem 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 "Constructor Problem" Watch "Constructor Problem" New topic
Author

Constructor Problem

Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
System.out.println("in extension"+i);
}
void add(int v) {
i =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();
}
}
Can anyone explain how is the output 22 and not 21 !!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Each time the method add is called it is through an object of type Base (see the formal parameter of bogo), and since Base is overridden by Extension it's Extension.add which is called.
So:
Initial value of Base.i gets set to 0.
Base is constructed: Extension.add called with
argument 1. Base.i gets set to 0 + 2*1 = 2.
Extension is constructed: Extension.add called with argument 2. Base.i gets set to 2 + 2*2 = 6.
Back in bogo Extension.add gets called with argument 8. Base.i gets set to 6 + 2*8 = 22.
-Barry
[ September 27, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
Yeah ! got it
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
I dont get this!! Can you explain things a little bit clearly???

Chitra
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Will the Base constructor not get called before Extension()??
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sorry, I have a heavy cold at the moment so perhaps my explanation was not so clear.
I have also run the code through a debugger and it does what I tried to explain.
Chitra, please quote my message indicating the parts you do not understand.
-Barry
[ September 27, 2002: Message edited by: Barry Gaunt ]
[ September 27, 2002: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Chitra:
Will the Base constructor not get called before Extension()??

Barry said:
Base is constructed: Extension.add called with
argument 1. Base.i gets set to 0 + 2*1 = 2.

Yes it does. I think I say that. The call to add in the Base constructor calls Extension.add.
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Hi Barry
A couple of println statements i inserted in the program showed me whats going on.
But I got confused because i thought that the
add() method defined inside Base constructor will call the one defined in the Base class.
Thanks
Chitra
 
Don't get me started about those stupid light bulbs.
 
subject: Constructor Problem
 
Similar Threads
Another question
Constructors & methods
constructor invocation-doubt
program output
How to read this program?