【发布时间】:2017-03-20 01:13:53
【问题描述】:
请在下面找到我的代码,我想澄清我关于覆盖和抛出异常的概念。下面提供的代码会起作用吗,如果是,那为什么。?我问这个问题是因为 IOException 和 ArithmeticException 之间没有直接的父子关系。
public class TestExceptionChild {
public static void main(String[] args) {
Parent parent = new Child();
try {
parent.msg();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Parent{
void msg() throws IOException{
System.out.println("parent");
}
}
class Child extends Parent{
void msg() throws ArithmeticException{
System.out.println("child");
}
}
【问题讨论】:
标签: java oop exception polymorphism overriding