请问一下大家,我在学习throws时发现子类在继承父类后,子类throws的异常为什么可以是父类所抛出异常的父类呢?
代码如下
package 异常;
public class Throws02 {
public static void main(String[] args) {
Son s = new Son();
try {
s.method();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}
class Father{
public void method() throws NullPointerException{
System.out.println("This is father");
}
}
class Son extends Father{
@Override
public void method() throws RuntimeException{
String[] arr = new String[3];
if(arr[2].equals("Tony")){
System.out.println(arr[2]);
}
}
}