This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
My question is on page 421, Q15 Chapter 5 of KB's book. I simplified the question into several codes below.
My first question is that would this example 1 compile? If not, I need to throw RuntimeException, correct? If yes, how main handles the exception?
My second question is that would this example 2 compile?
In example 3, class B extends A, which does not declare any exception. However, the overriding method doStuff() declares an exception. Is it fine to have the overriding method doStuff() (B.doStuff()) to declare an exception without its overridden method (A.doStuff()) declaring an exception? why and why not?
1-Example 2 will compile fine and will throw a run-time Arthimetic Exception.
2-Example 3 is violating the rules for overriding. You are basically overriding a instance method and this overridden
method can't throw an exception because your parent method is not throwing any exception.
example1: Will compile successfully, but, throws a RunTimeException when you run it.
example2: It will compile.
example3: will not compile as you have to declare the class A and the method both as abstract.