Correct Answer : Get Lastest Questions and Answer : Explanation: It will compile successfully. Because child class methods can have more restrictive exceptions. ParentException is a child exception of IndexOutOfBoundsException. However, as we know as per dynamic binding it will use the call method from child class. Hence, it will print com.hadoopexam.ParentException
Question : You have been given below code, what is the expected behavior?
Correct Answer : Get Lastest Questions and Answer : Explanation: The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
Question : You have been given below code, in their respective file.
public interface IParent { public abstract void draw(); }
class Parent implements IParent{ public void draw () { } }
public abstract class Child1 extends Parent { }
public class Child2 extends Parent { protected void draw (int color) { } }
public class GrandChild extends Parent implements IParent { public void resize() { } }
Correct Answer : Get Lastest Questions and Answer : Explanation: There is no issue with overriding and new method introduced in child classes. Code compiles successfully.
What if you try changing the throws clause? There are many ways to change the throws clause in the overriding method, including the following: a. Listing more general checked exceptions to throw. b. Listing more checked exceptions in addition to the given checked exception(s) in the base method.
. An overriding method cannot declare more checked exceptions in the throws clause than the list of exceptions declared in the throws clause of the base method. Why? The callers of the base method see only the list of the exceptions given in the throws clause of that method and will declare or handle these checked exceptions in their Code (and not more than that). . An overriding method can declare more specific exceptions than the exception(s) listed in the throws clause of the base method; in other words, you can The overriding method . Should have the same argument list types (or compatible types) as the base version. . Should have the same return type. . But from Java 5 onwards, the return type can be a subclass-covariant return types (which you'll learn shortly). . Should not have a more restrictive access modifier than the base version. . But it may have a less restrictive access modifier. . Should not throw new or broader checked exceptions. . But it may throw fewer or narrower checked exceptions, or any unchecked exception. . The names should exactly match. Remember that you cannot override a method if you do not inherit it. Private methods cannot be overridden because they are not inherited.