Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created January 19, 2014 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeevprasanna/8500769 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/8500769 to your computer and use it in GitHub Desktop.
polymorphism with exceptions
package polymorphism.superclassMethodInvoker.exception;
public class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
package polymorphism.superclassMethodInvoker.exception;
public class Dog2 {
public void eat() { /* no Exceptions */
}
// 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.
public static void main(String[] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error -
// unreported exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment