aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  program output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " program output" Watch " program output" New topic
Author

program output

shubhra chauhan
Greenhorn

Joined: Aug 31, 2007
Posts: 21
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.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

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 ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
shubhra chauhan
Greenhorn

Joined: Aug 31, 2007
Posts: 21
thanks Jesper...i got it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program output