• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQuest question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUESTION : 7
Imagine that there are two exception classes called Exception1 and Exception2 that descend from the Exception class. Given these two class definitions,
1. class First {
2. void test() throws Exception1, Exception2 { . . . }
3. }
4. class Second extends First {
5. void test() { . . . }
Create a class called Third that extends Second and defines a test() method. What exceptions can Third's test method throw? Select all valid answers.

a. Exception1
b. Exception2
c. No checked exception
d. any exceptions declared in the throws clause of the Third's test() method

The question is from JQuest and The answer is: c.

Why? Please explain in detail.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel,
This question has to do with rules of overriding methods that are declared with the throws clause. Remember when you override a method,

    * the overriding method can redeclare the exceptions thrown by the overridden method.
    * the overriding method can choose not to throw any exceptions thrown by the overridden method.
    * the overriding method cannot throw any exceptions that are broader than( ie., parents of ) the ones declared in the base class.

    In your example, the method in class Second does not declare any exceptions thrown in the base class First. Hence, if you were to extend the class Second( and call it Third ), its Test method cannot declare any exceptions in the thrown class.
    Hope this helps,
    Ajith
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic