What is the output for the below code ? import java.io.FileNotFoundException; class A{ public void printName() throws FileNotFoundException{ System.out.println("Value-A"); } } 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(); } }
What is the output for the below code ? import java.io.FileNotFoundException; class A{ public void printName() throws FileNotFoundException{ System.out.println("Value-A"); } } 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(); } } Correct Answer Name-B
Answer: Option 3
The overriding method can throw any unchecked (runtime) exception, regardless of exception thrown by overridden method. NullPointerException is RuntimeException so compiler will not complain.
মোঃ আরিফুল ইসলাম
Feb 20, 2025