• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Whiz Trial question

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code -

HTH,
- Manish
[ January 18, 2003: Message edited by: Manish Hatwalne ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic