Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whiz Trial question

 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code -

HTH,
- Manish
[ January 18, 2003: Message edited by: Manish Hatwalne ]
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic