i know how to determine if x instanceof aClass.
if x is not an instanceof aClass, how can i tell if it it will give me a compiler error or run time error.
Consider the following question~
interface I {
...
}
class A {
...
}
class B extends A {
...
}
class C implements I {
…
}
class D extends C {
...
}
and given the following declarations:
A aRef;
B bRef;
C cRef;
D dRef;
I iRef;
I i2Ref;
For each of the following statements circle all correct outcomes:
a. aRef = new B(); a) compiler error b) compiles c) can cause a runtime error
b. bRef = new A(); a) compiler error b) compiles c) can cause a runtime error
c. iRef = new C(); a) compiler error b) compiles c) can cause a runtime error
d. i2Ref = new D(); a) compiler error b) compiles c) can cause a runtime error
e. cRef = new I(); a) compiler error b) compiles c) can cause a runtime error
f. aRef = new C(); a) compiler error b) compiles c) can cause a runtime error
In the following, assume the right-hand side has been correctly instantiated to some legal value chosen
from the classes A, B, C, D but possibly different from the ones listed in (a) through (f).
g. bRef = (B)aRef; a) compiler error b) compiles c) can cause a runtime error
h. iRef = i2Ref; a) compiler error b) compiles c) can cause a runtime error
i. cRef = iRef; a) compiler error b) compiles c) can cause a runtime error
j. dRef = (D)iRef; a) compiler error b) compiles c) can cause a runtime error