Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

constructor invocation-doubt

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how does the following program work?

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

can someone plz explain
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Smarty
The line bogo(new Extension()) first creates a new Extension object before calling the bogo method. The Base class constructor of Extension class will execute first. Thus the constructor Base() will get executed. The Base() calls add(1) method. Now this call can be interpretted as this.add() sice the 'this' is an object of derived class its add method gets called ( polymorphism). So i will be set to 2. then derived class constructor is called so Extension(2) will be called. This causes i to be 6. Then in bogo method v have b.add(8) which causes i to become 22.
Please let me know if further clarification is needed.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the above code, you have to understand the overriding concept of methods.See the below example

For the above program,it will print derived three times.It will always call the print() in derived class,bcoz you have overridden it.

In the same way for the program which you gave ,at the statement

bogo(new Extension()); This will goto constructor of Extension and then u can assume super(); by default ,so it will goto base class constructor now it will call add(1),then the method in Extension class will be called.
Now i value is 2. Again it will come back to Extension constructor ,now it will call add(2) ,now the method in Extension will be executed and i value becomes 6. Now the creation of the instance (new Extension()) is over. It will go into the method. Now b.add(8) will be called.Now also the method in Extension class will be called,bcoz you have overridden the method add().i value becomes 22.

I think this explaination might have helped u.If I am wrong,correct me.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your explanation superb sir.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic