Due Tuesday, April 16, 11:59pm
class A {
int m1() {
return 1;
}
int m2() {
return this.m1();
}
}
class B extends A {
int m1() {
return 2;
}
}
class C extends B {
int m3() {
return this.m2();
}
int m4() {
return super.m1();
}
}
class D extends C {
int m1() {
return 3;
}
}
Using only your brain, your classmates, your lecture notes, and the
textbook (i.e., without using a Java compiler), write down the
result that is returned by each of the following expressions. (Note
that some of the expressions would not even compile because they are
ill-typed. Write "ill-typed" for these cases.)
(new A()).m1()
(new A()).m2()
(new A()).m3()
(new A()).m4()
(new B()).m1()
(new B()).m2()
(new B()).m3()
(new B()).m4()
(new C()).m1()
(new C()).m2()
(new C()).m3()
(new C()).m4()
(new D()).m1()
(new D()).m2()
(new D()).m3()
(new D()).m4()
For example, the fragment
(true ? new C() : new A()).m3()
will be rejected by the compiler but would not fail if we ran it. A
correct answer would be
"rejected / ok".
int i = (new A()).m1() + 5;
A a = new A(); int i = a.m3();
C c = new C(); int i = ((A)c).m1();
B b = new C(); int i = b.m3();
B b = (A)(new C());
B b = (C)(new A());
class M {
Integer p() {
return (new C()).m4();
}
}
C[] ca = new C[3]; B[] ba = ca; ba[0] = new B(); A[] aa = ba; int i = (aa[0]).m1();
C[] ca = (C[])(new A[3]); ca[0] = (C)(new A());
Submit an ascii email message with the usual subject line, in this format:
QUESTION 1:
1. answer
2. answer
...
QUESTION 2:
1. answer / answer
explanation
2. answer / answer
explanation
...
Sorry, no automatic grading.