Given that:
3. import java.io.*; 4. class Printer { 5. void print() throws IOException { } 6. } /*from ww w .ja v a 2s. c o m*/ 7. public class Main extends Printer { 8. public static void main(String[] args) { 9. new Main().print(); 10. } 11. // insert method here 12. }
Which of the following methods, inserted independently at line 11, compiles? (Choose all that apply.)
print()
throws Exception { }print()
throws FileNotFoundException { }print()
{ }print()
throws IOException { }print()
throws IOException { }print()
{ int x = 7/0; }B, C, D, and F are correct.
It's legal for overridden methods to have less restrictive access modifiers, to have fewer or narrower checked exceptions, and to have unchecked exceptions.
(Note: Of course, F would throw an exception at runtime.).
A is incorrect because Exception is broader.
E is incorrect because private is more restrictive.