Suggested Homework #8 (for Quiz #8 on 12/14/07 with Final)
Consider the Java code below:
interface A {
void m(A y);
}
interface C extends A {
A n(C y);
}
class B implements A {
int x;
void m(A y){
return;
}
}
class D extends B {
int y;
}
class E extends B implements C {
A n(C z){
return z;
}
}
(a) Name the types, classes, and interfaces that have been created.
(b) Using the notion of structural subtyping which of the
following is true ?
(i) A <: B
(ii) A <: C
(iii) B <: A
(iv) D <: C
(v) D <: B
(vi) E <: C
(vii) E <: B
(viii) E <: A
(c) Which of the following is true ?
- A inherits from B
- A inherits from C
- B inherits from A
- D inherits from C
- D inherits from B
- E inherits from C
- E inherits from B
- E inherits from A
(d) Which of the following methods typecheck ?
(i) void fa(A x){ x.m(x); };
(ii) void fb(B x){ x.n(x); };
(iii) void fc(C x){ x.n(x); };
(e) Suppose that there were methods fa,fb,fc with the signatures shown
in part (d). Which of the following pieces of code typecheck ? Explain.
(i) A a = A();
fa(a);
(ii) B b = B();
fa(b);
(iii) B b = B();
fc(b);
(iv) D d = D();
fa(d);
(v) D d = D();
fa(d);
fb(d);
fc(d);
(vi) E e = E();
fa(e);
fb(e);
fc(e);