import java.io.*; class Super { void show() { System.out.println("parent class"); } } public class Sub extends Super { void show() throws IOException //Compile time error { System.out.println("parent class"); } public static void main( String[] args ) { Super s=new Sub(); s.show(); } }As the method show() doesn't throws any exception while in Super class, hence its overriden version can also not throw any checked exception.
import java.io.*; class Super { void show(){ System.out.println("parent class"); } } public class Sub extends Super { void show() throws ArrayIndexOutOfBoundsException //Correct { System.out.println("child class"); } public static void main(String[] args) { Super s=new Sub(); s.show(); } }
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
import java.io.*; class Super { void show() throws ArithmeticException { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Cmpile time Error { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Labels: Core JAVA