• 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

mughal question,need help

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does the following code generate 22, can anyone explain
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();
}
}
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mary lee:
why does the following code generate 22, can anyone explain
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();
}
}



Hi Mary,
Dynamic binding (polymorphism) is in action here>
Note first that int i in Base class will be initialized with 0.
Now when you call bogo(new Extension()) in main a few things happen:
#1. Base's constructor is invoked. That will call add(1). Which add() is it? Is it a child's or is it a parent's? The answer lies in what object you instantiated. Have you just created an instance called Extension? So child's add() is invoiked and that will yield and answer of i = 2 (i += v*2, where v = 1).
#2. After Base's constructor, then comes Extension's constructor. Again the child's add() is invoked. And that will yield a value of i = 6 (i += v*2, where v = 2, and i = 2).
#3. Now look at bogo function. It is looking for an argument of type Base. But what did you pass in as its argument? Type Extension, right? or rather a copy of reference of type Extension. So then what does the add() from b.add(8) belong? Look at what is the true object reference you passed in? It is Extension even it is disguised as Base! So this add() got to be that of the child, subclass. Hence the statement i +=v*2 applies again. Therefore i = 22 (using i = 6, and v = 8).
Hope that helps
Lam
P.S. BTW, I have just answered similar question. Please use this jump for a bit more involved answer. http://www.javaranch.com/ubb/Forum24/HTML/009242.html

[This message has been edited by Lam Thai (edited April 15, 2001).]
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic