This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding doubt

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This block of code & description is from K & B book, chap:2 & page:102.
I have doubt in exception declaration. It is written in the book that overriding method can throw narrower or fewer exception or no exception, those declared in overridden method. Why below code does not compile, as the overriding method declares no exception. Can anyone please explain the concept behind of this?

If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method, the compiler assumes you�re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5).
Let�s take a look at an example:

This code will not compile because of the Exception declared on the
Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method, the compiler assumes you�re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception

Thats it, so to fix the code you need to handle or declare the exception.
for eg wrap the call of the method with try/catch Or make the main method throws the Exception.
[ September 27, 2007: Message edited by: Ahmed Yehia ]
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes this is because of polymorhism acting behind at compile time.

In polymorphic method calls ,which method can be invoked depends on the Reference type ,not run time type.Although method call itself depends on run type.

it all happens at compile time.Ploymorhism is resoled at compile time ,when compile have no informatin about runtime type or actual object type.

Since method invocations ->depends on Reference type,compile time,it throws exceptions,Compiler thinks invoked method may throw an exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic