• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor Problem

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah ! got it
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont get this!! Can you explain things a little bit clearly???

Chitra
 
Chitra Jay
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the Base constructor not get called before Extension()??
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic