Submitted by c-admin on Wed, 06/05/2019 - 00:23
Question:
What should be placed in the two blanks so that the following code will compile without errors:
class XXX{
public void m() throws Exception{
throw new Exception();
} }class YYY extends XXX{
public void m() {
} }public class TestClass {
public static void main(String[] args) {
______ obj = new ______();
obj.m();
} }
Select 1 option
A. XXX and YYY
B. XXX and XXX
C. YYY and YYY
D. YYY and XXX
E. Nothing will make the code compile.
Answer and Explanation (Click to expand) Explanation:
1. The overriding method may choose to have no throws clause even if the overridden method has a throws clause.
2. Whether a call needs to be wrapped in a try/catch or whether the enclosing method requires a throws clause depends on the class of the reference and not the class of the actual object.
Here, if you define s of type XXX, the call s.m() will have to be wrapped into a try/catch because main() doesn't have a throws clause. But if you define s of class YYY, there is no need of try catch because YYY's m() does not throw an exception.
Now, if the class of s is YYY, you cannot assign it an object of class XXX because XXX is a superclass of YYY. So the only option is to do: YYY s = new YYY();