• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Question from Khalid

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why this program prints out 22 when run?
What will be written to the standard output when the following program is run?
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();
}
}
Thanks.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a tricky question, because it settles some ascpects of overriding methods. Ok, let's go:

calls the constructor of Extension. This is:

Calling a constructor of this method first of all calls the default Constructor of the parent, b/c no (other) super()-constructor is called in the first line! This is:

This calls the method add, but which one? Of course the "special one" of the new Object of the class Extension (think of it: This example ist /not/ like you would implement Objects in the real world; think of simple Objects: The "Base" is "car", the special is "Jaguar". Constructing a "default" car sets its default maxSpeed to 50, but constructing a "Jaguar" will set its default maxSpeed to 100; so you would have to override the setDefaultMaxSpeed-method (which could be called in the constructor), but you would /not/ have to change the constructor (or you would have to set the member maxSpeed to 100 in the constructor, with no setDefaultMaxSpeed-method, and you would not have to change any method). Got it?)
So add(1) of the Extension-class is called. Remember: Members of a class have a default value, so i is by default 0. add(1) calls i += 1*2, so after this, i = 2 holds.
/Now/ the implementation of the constructor of Extension will be handled; add(2) is called, and, no surprise at all, this is the add-method of Extension. Now: i += 2*2, so i = 2 + 4 = 6 holds.
This is the end of the construction of the new Extension-Object. Now bogo is called - with an Extension as the passed object (not quite right - with a copy of the pointer to the new Extension object as the passed variable).
In Bogo, the "add"-method is called - but which one? This has to be decided at runtime: The JVM looks, "what kind of Base" it has to handle (think of the example given above - "what kind of car do I handle?"). Here, it has to handle a "special car" with its special method. Again, the add-method of the Extension-class is called. Now: i += 8 * 2, so: i = 6 + 16 = 22 holds.
That's it.
Hope it helps
Detlev
(Hint: you can find out this by tracing such a program line by line, that often helps to understand what the JVM does and how the concepts work.)
[This message has been edited by Detlev Beutner (edited July 16, 2001).]
 
Sarah Haideri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the detailed explanation.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what happens when the code is run -
In main() an instance of Extension is passed to bogo . This instance of Extension when passed to bogo results in the Extension object being cast to Base which in turn results in the Base constructor being called . But the add in the Base constructor calls the add() in the Extension class . This is probably 'cause the object passed is of Extension type & add is overridden in it . So now the value of i = 2.
Next the Extension constructor is called (I don't know why !) . The add of the Extension is called here which results in i = 6 .
Since objects are passed by reference the Extension object passed in bogo can be now reffered to by b.
b.add(8) calls the Extension's add method which gives the final value of i = 22 . Note that if add was not overridden in Extension the final value of i would've been 11 .
Since print() is not overridden in Extension the super-class version of print is called & i is printed .

U can see the flow of execution if u have println() statements at the entry & exit of the constructors & methods .
One thing that bugs me though is that the Extension constructor should be called before the Base constructor being called since
new Extension() //initiates a call to the Extension constructor
& if this were to happen i would've been 21 .
Can someone elaborate on this ?
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashish:
Have a look at my answer:
"Calling a constructor of this method first of all calls the default Constructor of the parent, b/c no (other) super()-constructor is called in the first line!"
That's all.
Hope it helps again ;-)
Detlev
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic