• 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

overridden not required to declare exception?

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't understand why overridden method not required to declare exception that declared in superclass method?

it's like subclass doesn't fullfil the contract. any thought?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the superclass method does not have to throw the exception, does it? So the overridden method in the subclass does not have to throw it (or declare it throws it).
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are overriding a method, it does not mean that your method is going to throw the exception. Your implementation may be totally different than that of the overridden method or you may decide to handle the exceptions in your method instead of passing it on. Thus you are not required to throw the same exceptions. However, you may not throw broader or new exceptions, as this might break someone else's code that is calling your method through polimorphism.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

If the above statements(by other memebers in this post) being made about overriding is true, then anyone could please explain me the code in exam watch block on pg 104 of k&B book.

The eat method in subclass Dog2, doesn't declares the exception being declared by its superclass eat method. Hence giving error.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error on page 104 of K&B is for the line that reads a.eat(). The type of the variable 'a' is Animal. The method eat() in Animal declares an exception. To get the code to compile, the method call needs to be in a try-catch block.

Declarations with exceptions are a separate issue. Leave out unchecked exceptions for purposes of discussion. It is legal for an overriding method to declare fewer exceptions than the method it overrides because any code that uses the override would still work -- the worst that would happen is that it would contain a seemingly redundant try-catch block. In terms of K&B page 104:
Animal a = new Dog2();
...
try {
a.eat();
}
catch (Exception ex) {
assert (false) : "I'll never get here";
}
 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't understand that why the error is shown beacuase of the Exception declared on the Animal eat() method; even though at run time the eat() method used would be of the Dog version for refernce variable a.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Namit Puri:
I still can't understand that why the error is shown beacuase of the Exception declared on the Animal eat() method; even though at run time the eat() method used would be of the Dog version for refernce variable a.



In that exam watch, we refer Dog object as Animal type. Imagine when we have method with Animal type argument, someone can pass in Dog or Animal object type. Compiler take precaution in this case.

To summarise: At compile time, compiler only knows/cares that we invoke eat() method of Animal type. Compiler doesn't know what the real object is. It's JVM job.
reply
    Bookmark Topic Watch Topic
  • New Topic