Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Throws exceptions in child and parent classes

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check this code :


this code will not compile unless we add try/ catch or throws declaration in main !
My question is h.book(); will use the overriden method in SuperHotel but this methods doesn't throw anything so why shoud we handle an exception that's throwed in parent class ??
 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since h is a Hotel type it will always be treated as a Hotel. If it were a SuperHotel type then you would not have to handle the exception.
 
Adam Satyres
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Mercier wrote:Since h is a Hotel type it will always be treated as a Hotel. If it were a SuperHotel type then you would not have to handle the exception.


Yes you are right,but the method has been overriden ! why the exception still exist
 
Greenhorn
Posts: 1
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is correct that the book() method is overridden in the SuperHotel class, but because h is declared as a Hotel the compiler expects any caller of the book method to handle the checked exception. In the code below, the exception is surrounded a try/catch, but because the book() method is overridden, the exception will never be thrown.

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler sees that h is of reference type Hotel, so it looks at Hotel declarations at compile time... At runtime is when the actual object type will be used.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic