• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

program output

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}

output of this program is 22. how? need an explanation.
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shubhra chauhan:
class Base {
int i;
Base() { add(1); }
void add(int v) { i += v; }
void print() { System.out.println(i); }
}
class Extension extends Base {
Extension() {
super();//implicitly calls Base() - look above
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();
}
}

output of this program is 22. how? need an explanation.


[ September 17, 2007: Message edited by: Faisal Pasha ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: Please use code tags when you post code, so that the forum can display your code properly.

One way to find out what exactly the flow of a program like this is, is by running it in a debugger. Start the program in the debugger and step through it line by line to see where it goes and what happens to variables.

This program works like this:

1. First, an Extension object is created. Part of the process of initializing the new Extension object is initializing the Base part of the object. That's what happens first. Initially, the member variable i in class Base is initialized with a default value of 0. Then the Base constructor is run, which calls add(1). Note that since we're creating an Extension object, the add() method in class Extension is called (not the add method in class Base!), which sets i to 0 + 1 * 2 = 2.

2. Now the Extension part of the object is initialized. The Extension constructor calls add(2). The add() method in class Extension is called again, so after this, i is set to 2 + 2 * 2 = 6.

3. Then, in method bogo(), add(8) is called on the object. Since b refers to an Extension object and because of polymorphism, add() in class Extension is called (even though b is a Base). So after that call, i is set to 6 + 8 * 2 = 22.

4. print() in class Base is called and the value 22 is printed.
 
shubhra chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jesper...i got it..
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic