0

I have two codes. In first code I am getting compile time error in java and second works fine . Why is it happening so **First Code **

import java.io.*;  
class Parent{  
  void msg(){System.out.println("parent");}  
}  
  
class TestExceptionChild extends Parent{  
  void msg()throws IOException{  
    System.out.println("TestExceptionChild");  
  }  
  public static void main(String args[]){  
   Parent p=new TestExceptionChild();  
   p.msg();  
  }  
}  

second code

import java.io.*;  
class Parent{  
  void msg(){System.out.println("parent");}  
}  
  
class TestExceptionChild1 extends Parent{  
  void msg()throws ArithmeticException{  
    System.out.println("child");  
  }  
  public static void main(String args[]){  
   Parent p=new TestExceptionChild1();  
   p.msg();  
  }  
}  
  • 1
    [This](https://stackoverflow.com/a/9036060/6650475) should help. – Andrew S Jul 22 '21 at 15:46
  • Think about that code for a while: `Parent p = getSomeParent(); p.msg();` From a compiler's point of view, could you expect a checked exception being thrown here? – Seelenvirtuose Jul 22 '21 at 16:06

0 Answers0