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.
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.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
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.
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 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
原文:http://blog.csdn.net/lantianjialiang/article/details/19476369