this question is from K&B Self Tests....
i could not understand the solution..any help is appreciated
1. class
Test {
2. public static Foo f = new Foo();
3. public static Foo f2;
4. public static Bar b = new Bar();
5.
6. public static void main(
String [] args) {
7. for (int x=0; x<6; x++) {
8. f2 = getFoo(x);
9. f2.react();
10. }
11. }
12. static Foo getFoo(int y) {
13. if ( 0 == y % 2 ) {
14. return f;
15. } else {
16. return b;
17. }
18. }
19. }
20.
21. class Bar extends Foo {
22. void react() { System.out.print("Bar "); }
23. }
24.
25. class Foo {
26. void react() { System.out.print("Foo "); }
27. }
what is the result?
ans is Foo Bar Foo Bar Foo Bar
my guess is that it should be Bar Bar Foo Bar Foo Bar
as overridden methods are called based on runtime type of the object ....