Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Overriden and Overriding Methods Declaration Conundrum.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,


I'm very confused with the following piece of code that I have which fails to compile -


This fails to compile due to the checked exception declaration in the superclass method eat()

I summarize the following rule of overriding concerning checked exceptions below

1) The overriding method MUST not throw any new or broader checked exceptions than those declared by the method in the superclass.

If I reason with this rule at the back of my mind, this code SHOULD compile. Furthermore, in the code the subclass object is assigned to a superclass reference which means in this case the subclass version of the eat method should execute successfully. Can anyone point me out of this dilemma?

Regds,
Abhijit.
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method eat in the Animal class throws a Checked Exception. So it's necessary for you to handle it during compile time. The compiler will not know which object Animal reference will be pointing to.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to that the compiler will not complain if its a RuntimeException.
 
Marshal
Posts: 77531
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that the Exception is unchecked does not make it better design. It simply means you can confuse the compiler.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The fact that the Exception is unchecked does not make it better design. It simply means you can confuse the compiler.


You are right... I just want to mention that the compiler error was due to the Checked Exception and not based on the overriding concept.
 
Campbell Ritchie
Marshal
Posts: 77531
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could have sworn I had seen the same code in a similar question only a few weeks ago, and I was mistaken about the Exceptions. But I can't seem to find it on a search. Maybe you will be more successful than me; try searching.
 
Destroy anything that stands in your way. Except this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic