C
Chad
Given the following...
class X {
void f1() { System.out.println("XXX");}
void f2() { System.out.println("AAA"); f1();}
}
class Y extends X {
void f1() { System.out.println("YYY"); }
}
class Z extends X {
void f1() { System.out.println("ZZZ"); }
}
public class Main {
static void g(X a) {
a.f2();
}
public static void main(String[] args) {
X x = new X();
Y y = new Y();
Z z = new Z();
Object obj = new Y();
((X) obj).f2();
g(z);
//y = (Y)x;
}
}
I get the following output..
AAA
YYY
AAA
ZZZ
The question is about
Object obj = new Y();
((X) obj).f2();
How come YYY, but not XXX, gets printed on the second line? Both f1()
and f2() are in class X. So shouldn't f1() also have been casted to
(type) X?
Chad
class X {
void f1() { System.out.println("XXX");}
void f2() { System.out.println("AAA"); f1();}
}
class Y extends X {
void f1() { System.out.println("YYY"); }
}
class Z extends X {
void f1() { System.out.println("ZZZ"); }
}
public class Main {
static void g(X a) {
a.f2();
}
public static void main(String[] args) {
X x = new X();
Y y = new Y();
Z z = new Z();
Object obj = new Y();
((X) obj).f2();
g(z);
//y = (Y)x;
}
}
I get the following output..
AAA
YYY
AAA
ZZZ
The question is about
Object obj = new Y();
((X) obj).f2();
How come YYY, but not XXX, gets printed on the second line? Both f1()
and f2() are in class X. So shouldn't f1() also have been casted to
(type) X?
Chad