Welcome Guest | Sign in | Register
Inheritance - Discussion Page For Q.2837 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Inheritance > Questions and Answers

Q: What is the output for the below code ?

import java.io.FileNotFoundException;
public class A {
public void printName() throws FileNotFoundException {
System.out.println("Value-A");
}
}
public class B extends A{
public void printName() throws NullPointerException{
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) throws Exception{
A a = new B();
a.printName();
}
}
A. Value-A B. Compilation fails-Exception NullPointerException is not compatible with throws
clause in A.printName()
C. Name-B D. Compilation succeed but no output

Answer and Explanation

Answer:Name-B

Explanation:
The overriding method can throw any unchecked (runtime) exception, regardless of
exception thrown by overridden method. NullPointerException is RuntimeException so compiler not complain

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.