Java中overriding的规则

Rules for method overriding:

  • The argument list should be exactly the same as that of the overridden method.

  • 参数列表必须一致

  • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.

  • return的类型可以一样,或者子类returen的类型是父类的子类

  • The access level cannot be more restrictive than the overridden method’s access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.

  • 访问级别只能比父类的更宽松;Java中的访问级别从高到低依次是:public, package level(具体使用时什么都不写),protected, private

  • Instance methods can be overridden only if they are inherited by the subclass.

  • 实例方法只能覆盖从父类继承的方法

  • A method declared final cannot be overridden.

  • final方法不能被覆盖

  • A method declared static cannot be overridden but can be re-declared.

  • static方法不能被覆盖,但是可以被重新声明

  • If a method cannot be inherited, then it cannot be overridden.

  • 如果方法不能继承,方法野不能覆盖

  • A subclass within the same package as the instance’s superclass can override any superclass method that is not declared private or final.

  • 如果子类扶父类在同一个包下,子类可以覆盖父类的所以非private或final的方法

  • A subclass in a different package can only override the non-final methods declared public or protected.

  • 如果子类扶父类在不同的包下,子类可以覆盖父类的所以非final同时public/protected的方法

  • An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

  • 不管父类有没有抛出异常,子类覆盖的方法可以抛出不检查的异常;如果父类抛出了异常,那么子类覆盖的方法只能抛出比父类范围更小的异常

  • Constructors cannot be overridden.

  • 构造函数不能覆盖

如果对上面的说明还是不理解,可以参加下面的例子:

1. 父类

public class TestA {

    protected void test1(int a, int b) {
        System.out.println("supper");
        return;        
    }

    protected long test2(int a, int b) {
        System.out.println("supper");
        return 0;        
    }

    protected TestB test3(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test4(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test5(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test6(int a, TestA b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test6(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected static TestA test7(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test8(int a, int b) {
        System.out.println("supper");
        return null;        
    }

    protected TestA test9(int a, int b) throws Exception{
        System.out.println("supper");
        return null;        
    }

    protected TestA test10(int a, int b) throws NullPointerException{
        System.out.println("supper");
        return null;        
    }

    protected static TestA test11(int a, int b) {
        System.out.println("supper");
        return null;        
    }

}

2. 子类

public class TestB extends TestA{

    @Override
    public void test1(int a, int b) {
        System.out.println("sub");
        return;        
    }

    @Override
    public long test2(int a, int b) {
        System.out.println("sub");
        return 0;        
    }

// fail    
//    @Override
//    protected TestA test3(int a, int b) {
//        System.out.println("sub");
//        return null;        
//    }

    @Override
    protected TestB test3(int a, int b) {
        System.out.println("sub");
        return null;        
    }

    @Override
    protected TestB test4(int a, int b) {
        System.out.println("sub");
        return null;        
    }

    @Override
    public TestB test5(int a, int b) {
        System.out.println("sub");
        return null;        
    }

//    fail
//    @Override
//    protected TestA test6(int a, TestB b) {
//        System.out.println("sub");
//        return null;        
//    }

    @Override
    protected TestA test6(int a, TestA b) {
        System.out.println("sub");
        return null;        
    }

    @Override
    protected TestA test6(int a, int b) {
        System.out.println("sub");
        return null;        
    }

//    fail: This instance method cannot override the static method
//    protected TestA test7(int a, int b) {
//        System.out.println("sub");
//        return null;        
//    }

//    fail: can not throw checked exception
//    @Override
//    protected TestA test8(int a, int b) throws Exception {
//        System.out.println("sub");
//        return null;        
//    }

    @Override
    protected TestA test8(int a, int b) throws NullPointerException {
        System.out.println("sub");
        return null;        
    }

// fail:Exception Throwable is not compatible with throws 
//     clause in TestA.test8(int, int)
//    @Override
//    protected TestA test8(int a, int b) throws Throwable {
//        System.out.println("sub");
//        return null;        
//    }


//    OK
//    @Override
//    protected TestA test9(int a, int b) throws Exception{
//        System.out.println("sub");
//        return null;        
//    }
//    OK
//    @Override
//    protected TestA test9(int a, int b) throws Error{
//        System.out.println("sub");
//        return null;        
//    }

    @Override
    protected TestA test9(int a, int b) throws NullPointerException{
        System.out.println("sub");
        return null;        
    }

//    fail
//    @Override
//    protected TestA test9(int a, int b) throws Throwable{
//        System.out.println("sub");
//        return null;        
//    }

    @Override
    protected TestA test10(int a, int b) throws NullPointerException{
        System.out.println("sub");
        return null;        
    }

//    fail
//    @Override
//    protected TestA test10(int a, int b) throws Exception{
//        System.out.println("sub");
//        return null;        
//    }

    protected static TestA test11(int a, int b) {
        System.out.println("supper");
        return null;        
    }


}

http://www.tutorialspoint.com/java/java_overriding.htm

http://docs.oracle.com/javase/tutorial/java/IandI/override.html