若想實現一個合格重寫方法,而不是重載,那么必須同時滿足下面的要求!
重寫規則之一:重寫方法不能比被重寫方法限制有更嚴格的訪問級別。
(但是可以更廣泛,比如父類方法是包訪問權限,子類的重寫方法是public訪問權限。) 比如:Object類有個toString()方法,開始重寫這個方法的時候我們總容易忘記public修飾符,編譯器當然不會放過任何教訓我們 的機會。出錯的原因就是:沒有加任何訪問修飾符的方法具有包訪問權限,包訪問權限比public當然要嚴格了,所以編譯器會報錯的。
重寫規則之二: 參數列表必須與被重寫方法的相同。
重寫有個孿生的弟弟叫重載,也就是后面要出場的。如果子類方法的參數與父類對應的方法不同,那么就是你認錯人了,那是重載,不是重寫。
重寫規則之三:返回類型必須與被重寫方法的返回類型相同。
父類方法A:void eat(){} 子類方法B:int eat(){} 兩者雖然參數相同,可是返回類型不同,所以不是重寫。
父類方法A:int eat(){} 子類方法B:long eat(){} 返回類型雖然兼容父類,但是不同就是不同,所以不是重寫。
重寫規則之四:重寫方法不能拋出新的異常或者比被重寫方法聲明的檢查異常更廣的檢查異常。但是可以拋出更少,更有限或者不拋出異常。
注意:這種限制只是針對檢查異常,至於運行時異常RuntimeException及其子類不再這個限制之中。
重寫規則之五: 不能重寫被標識為final的方法。
重寫規則之六:如果一個方法不能被繼承,則不能重寫它。如private方法
比較典型的就是父類的private方法。下例會產生一個有趣的現象。
public class Test {
public static void main (String[] args) {
//Animal h = new Horse();
Horse h = new Horse();
h.eat();
}
}
class Animal {
private void eat(){
System.out.println ("Animal is eating.");
}
}
class Horse extends Animal{
public void eat(){
System.out.println ("Horse is eating.");
}
}
這段代碼是能通過編譯的。表面上看來違反了第六條規則,但實際上那是一點巧合。Animal類的eat()方法不能被繼承,因此Horse類中的 eat()方法是一個全新的方法,不是重寫也不是重載,只是一個只屬於Horse類的全新的方法!這點讓很多人迷惑了,但是也不是那么難以理解。
main()方法如果是這樣:
Animal h = new Horse();
//Horse h = new Horse();
h.eat();
編譯器會報錯,為什么呢?Horse類的eat()方法是public的啊!應該可以調用啊!請牢記,多態只看父類引用的方法,而不看子類對象的方法!
重寫規則之七:子類不能用 靜態方法 重寫 父類的非靜態方法
編繹無法通過this static method cannot hide the instance mehtod from
class A {
protected int method1(int a, int b) {
return 0;
}
}
public class Test1 extends A {
private int method1(int a, long b) {
return 0;
}
//this static method cannot hide the instance mehtod from A
static public int method1(int a, int b) {
return 0;
}
}
重寫規則之八:子類不能用 非靜態方法 重寫 父類的靜態方法
編繹報錯:this instance method cannot override the static mehtod from A
class A {
protected static int method1(int a, int b) {
return 0;
}
}
public class Test1 extends A {
private int method1(int a, long b) {
return 0;
}
//this static method cannot hide the instance mehtod from A
//this instance method cannot override the static mehtod from A
public int method1(int a, int b) {
return 0;
}
}
