throws子句在继承当中overrride时有什么规则?

throws子句在继承当中overrride时的规则
马克-to-win:当子类方法override父类方法时,throws子句不能引进新的checked异常。换句话说:子类override 方法的throws子句checked异常不能比父类多。马克-to-win:上面一条是死语法规定,这种规定,实际上都是源于checked异常这种最初的设计。
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



例:1.8.1-本章源码

import java.io.IOException;
class Animal{
    void call() throws IOException
    {
        System.out.println("Animal");
    }
}
class Dog extends Animal{
    void call() throws IOException
    {
        System.out.println("Dog");
    }
}

public class Test {
    public static void main(String args[]) throws IOException {
        Dog d = new Dog();
        d.call();
    }
}

输出结果:
Dog


马克-to-win:为了让读者更加清楚一点,现在我们假象程序员在编Dog的时候就突发奇想想加一个SQLException,会发生什么?




例:1.8.2(有问题不能编译)-本章源码

import java.io.IOException;
import java.sql.SQLException;
class Animal{
    void call() throws IOException
    {
        System.out.println("Animal");
    }
}
class Dog extends Animal{
    /*马克-to-win:这里报错说父类的throws得加上SQLException,Exception SQLException is not compatible with throws clause in Animal.call()*/   
    void call() throws IOException, SQLException
    {
        System.out.println("Dog");
    }
}

public class Test {
    public static void main(String args[]) throws IOException {
        Dog d = new Dog();
        d.call();
    }
}