Created
January 19, 2014 05:22
-
-
Save rajeevprasanna/8500769 to your computer and use it in GitHub Desktop.
polymorphism with exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package polymorphism.superclassMethodInvoker.exception; | |
public class Animal { | |
public void eat() throws Exception { | |
// throws an Exception | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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