posted 16 years ago

can somebody walks me through this?

------------

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();

}

}

A. 9

B. 18

C. 20

D. 21

E. 22

Answer: E

Thanks,

yuki

------------

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();

}

}

A. 9

B. 18

C. 20

D. 21

E. 22

Answer: E

Thanks,

yuki

posted 16 years ago

Hi Yuki,

Let's walk together.

1 - bogo() is invoked in main()

2 - Extension object is instantiated via 'new' op

3 - Extension's constructor is called

4 - inside Extension(),

5 - Base's constructor is called

6 - int i is initialized to 0

7 - add(1) is called (note: overriding effect; this is the method add() in Extension). So i = 2 since i += v*2, where i = 0, v = 1

8 - Base() is done

9 - back to Extension's constructor and call add(2). So i = 6 since i += v*2, where i = 2 and v = 2

10 - Extension constructor is done

11 - Back to bogo() and call add(8). So i = 22 since i += v*2, where i = 6 and v = 8

12 - print() is called and that how you got the answer of i = 22.

Hope that helps,

- Lam -

[This message has been edited by Lam Thai (edited May 11, 2001).]

Originally posted by Yuki Cho:

can somebody walks me through this?

------------

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();

}

}

A. 9

B. 18

C. 20

D. 21

E. 22

Answer: E

Thanks,

yuki

Hi Yuki,

Let's walk together.

1 - bogo() is invoked in main()

2 - Extension object is instantiated via 'new' op

3 - Extension's constructor is called

4 - inside Extension(),

**implicit**super() is called

5 - Base's constructor is called

6 - int i is initialized to 0

7 - add(1) is called (note: overriding effect; this is the method add() in Extension). So i = 2 since i += v*2, where i = 0, v = 1

8 - Base() is done

9 - back to Extension's constructor and call add(2). So i = 6 since i += v*2, where i = 2 and v = 2

10 - Extension constructor is done

11 - Back to bogo() and call add(8). So i = 22 since i += v*2, where i = 6 and v = 8

12 - print() is called and that how you got the answer of i = 22.

Hope that helps,

- Lam -

[This message has been edited by Lam Thai (edited May 11, 2001).]

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |