• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling in overriding a method  RSS feed

 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets consider a code


Since reference variable of type Animal is pointing to a Horse Object, when eat() is invoked it invokes the overrided method eat() which prints Cow eats

Now consider this code



If we consider the previous code the ref variable clearly invokes the overrided method. But in this code why does it call the superclass method and produce an error? Since no exceptions are thrown it is less broader than the super class exception. Why does this produce error? Can someone please clearly explain it to me? I cannot understand how exceptions work in overriding.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working out exactly which version of the method will be called is done at runtime, and depends on the actual type of the object. But checking of checked exceptions is done by the compiler. And the compiler only considers the type of the reference.

So in this case the compiler has no idea what the actual type is going to be. All it knows is that you're calling eat() on an Animal reference. And it knows that the eat() method of an Animal might throw an Exception. This is a checked exception, so it's an error if you're not handling it.

It's an error for exactly the same reason as this is an error:
Here it's trying to call a method that isn't defined in the reference type of the variable. Yes, the moo() method exists in the Cow class, but as far as the compiler is concerned it could be any Animal.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we consider the previous code the ref variable clearly invokes the overridden method. But in this code why does it call the super class method and produce an error? Since no exceptions are thrown it is less broader than the super class exception. Why does this produce error? Can someone please clearly explain it to me? I cannot understand how exceptions work in overriding.


The correct approach is quintessential in isolating the problem in such cases. Approach such cases in the following manner:

a) Method overriding comes in to play only at run time i.e. if your code compiles correctly and then you get different behavior when you run it depending on the type of object on the heap. There is no overriding in play at compile time in the second example you quoted. So, clearly it has got to do something with checked exception handling and NOT method overriding.

b) All other method calls are resolved at compile time based on the reference type of the object. Compiler checks for checked exceptions to be handled or declared at compile time. It sees that the Animal class eat() method declares a checked exception of type Exception. So, it shall force you to either handle or declare the same when you call that method. That is a rule for handling checked exceptions in Java language.

Let me know if you have any further doubts regarding the same.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler always check the super class method first before executing the sub class method. and if there is a possibility that it throws the checked exception then it gives a compile time error before executing further.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:Compiler always check the super class method first before executing the sub class method. and if there is a possibility that it throws the checked exception then it gives a compile time error before executing further.


That's not true in general, as the following code would show:
That will compile fine - no checking of the superclass method is involved. The difference is the reference type of the variable - and that's the version the compiler checks regardless of what superclasses might exist.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a hard time understanding what gets decided at compile time and what at runtime. Now I am clear. Thanks for the answers and the very helpful tip regarding the runtime/compile time actions.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's not true in general, as the following code would show:


thanks for correcting i actually meant somewhat that only..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!