• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

polymorphic supertype reference exceptions


I was reading folowing lines and I did not understand clearly.
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 are 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
Any ideas, resources,sample code,links, highly appreciated. thanks in advance
0
gudii9
Asked:
gudii9
2 Solutions
 
for_yanCommented:

But they have a very good example there:
for
d.eat() you should not catch exception because
eat() method of Dog class does not throw exception

for
a.eat() you  should  catch exception ,because
compiler assumes you are calling a method of class Animal
even though a was instantiated as Dog, so compiler will
requuire that you catch Exception


class Animal {
  public void eat() throws Exception {
    // throws an Exception
  }
}
class Dog2 extends Animal {
  public void eat() { // no Exceptions }
  public static void main(String [] arga) {
    Animal a = new Dog2();
    Dog2 d = new Dog2();
    d.eat();           // ok
    a.eat();           // compiler error -
                       // unreported exception
  }
}

Open in new window

0
 
objectsCommented:
that question is really well covered here

http://wiki.answers.com/Q/What_is_method_overriding_in_java
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now