| Author |
Whiz Trial question
|
Maria Garcia
Ranch Hand
Joined: Jul 14, 2002
Posts: 86
|
|
The class AssertTest defines a method methodA(int i) as given below. Which of the following declarations of methodA(int i) are valid in the subclass of AssertTest? a)public void methodA(int i) b)public void methodA(int i) throws Exception c)public void methodA(int i) throws Throwable d)public void methodA(int i) throws Error e) A is the only answer The answer given was A and D. Since Error is the superclass of AssertionError, the overriding method should not be allowed to throw Error right ? [ January 17, 2003: Message edited by: Maria Garcia ]
|
SCJP 1.4
|
 |
chi Lin
Ranch Hand
Joined: Aug 24, 2001
Posts: 348
|
|
this is right !
Originally posted by Maria Garcia: The class AssertTest defines a method methodA(int i) as given below. Which of the following declarations of methodA(int i) are valid in the subclass of AssertTest? a)public void methodA(int i) b)public void methodA(int i) throws Exception c)public void methodA(int i) throws Throwable d)public void methodA(int i) throws Error e) A is the only answer The answer given was A and E. Since Error is the superclass of AssertionError, the overriding method should not be allowed to throw Error right ?
|
not so smart guy still curious to learn new stuff every now and then
|
 |
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
|
|
An overridding method is not allowed to throw more checked exceptions than those declared in the overridden method. Those not allowed include both new ones not declared and non-subclasses of the declared checked exceptions. The reason is that if a code written originally for a Base class knows how to deal with the exceptions declared in the methods it calls; it could not forsee (manage) the new ones added by subclasses. This would break polymorphism: an instance of a subclass is not suitable for wherever an instance of the superclass is ok --Substitution principle. Thus b is also wrong, though Exception is not a supertype of AssertionError, but a checked exception.
|
SCJP2. Please Indent your code using UBB Code
|
 |
Manish Hatwalne
Ranch Hand
Joined: Sep 22, 2001
Posts: 2573
|
|
Try this code - HTH, - Manish [ January 18, 2003: Message edited by: Manish Hatwalne ]
|
 |
Sarma Lolla
Ranch Hand
Joined: Oct 21, 2002
Posts: 203
|
|
When I tested both these are allowed by the compiler. public void methodA(int i)throws RuntimeException public void methodA(int i)throws Error. How ever public void methodA(int i)throws Exception public void methodA(int i)throws Throwable both are giving error. Can some one explain these things?
|
 |
sam huang
Greenhorn
Joined: Dec 31, 2002
Posts: 28
|
|
Dear all, I still don't know which is the right answer. Could you please give me some explanation? I think only A or E are the right answer. Sam
|
SCJP,SCWCD,SCBCD,MCSE
|
 |
 |
|
|
subject: Whiz Trial question
|
|
|