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, privateInstance 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.
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 Tes