• 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

Method Overriding : Throwing Exceptions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this Question while preparing for OCAJP

Q.

How can you fix the following code to make it compile:


Assume that changes suggested in a option are to be applied independent of other options.

Options:
A>Change doStuff in Amazing to throw only IllegalArgumentException.
B>Change doStuff in Great to throw only FileNotFoundException as well as IllegalArgumentException.
C>Change doStuff in Amazing to throw only IOException.
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException.
E>Replace g.doStuff() to ((Amazing) g).doStuff().

Corrent Answers are A and D. But, why can't it be C and D?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SCJP 6 book by Kathe Sierra explains it nicely as:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.



In your example C is not valid as IOException is a super class of FileNotFoundException.
 
samarth mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:The SCJP 6 book by Kathe Sierra explains it nicely as:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.



In your example C is not valid as IOException is a super class of FileNotFoundException.



I would like you to read the options once again.
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

samarth mishra wrote:
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.


Yes but option C doesn't meet that criteria.

The super class would throw FileNotFoundException and the sub class would throw IOException
 
samarth mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

samarth mishra wrote:
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.


Yes but option C doesn't meet that criteria.

The super class would throw FileNotFoundException and the sub class would throw IOException



These are the 2 options which say that for super and sub class only IOException will be thrown.

C>Change doStuff in Amazing to throw only IOException. {Amazing Class wchich is the Subclass throws IOException}
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException. {Great Class which is the Superclass throws IOException}
So, I am asking what is wrong with it.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

samarth mishra wrote:These are the 2 options which say that for super and sub class only IOException will be thrown.

C>Change doStuff in Amazing to throw only IOException. {Amazing Class wchich is the Subclass throws IOException}
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException. {Great Class which is the Superclass throws IOException}
So, I am asking what is wrong with it.


Oh I think I see what you mean which is if you do both C & D the code is valid.
Read the question again, you haven't understood it.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I interpreted option C as: Change the Subclass "Amazing" so that it no longer throws IllegalArgumentException.

That would be correct if IllegalArgumentException was a checked exception, because the superclass does not declare it.
However because IllegalArgumentException is a runtime exception, it isn't actually causing any problem.
Making this change wouldn't remove the compile error in the code as given.

Parent class throws FileNotFoundException
Child class throws IOException --> error because IOException is broader than FileNotFoundException

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone explain me why B is not correct as well? "Change doStuff in Great to throw FileNotFoundException and IllegalArgumentException." That would fix the overriding problem with doStuff method in Amazing class and it wouldn't get a compile error in main method because it throws an IOException wich is a parent of FileNotFoundException. What am I not getting right here?

Sorry for reopening a thread.

Cheers.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have it backwards. A method in a subclass cannot throw a more general checked exception than any of its superclasses. IOException is more general than FileNotFoundException. Therefore, Amazing.doStuff cannot legally declare that it throws IOException because Great.doStuff only throws FileNotFoundException.

This has to do with the Liskov Substitution Principle. Allowing Amazing.doStuff to throw IOException when it's superclass can only throw FileNotFoundException violates LSP.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does throwing an IllegalArgumentException. If the superclass method accepts all arguments, then the subclass method must accept all arguments. The difference is that the IllegalArgumentException is an unchecked exception and the javac tool does not notice it. Just because the code compiles does not imply there is anything correct about it.
 
A. Costa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I just misread that. Thanks a million.
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic