答案是,不會(huì)。子類優(yōu)先實(shí)現(xiàn)父類的方法,雖然父類的方法和接口的方法長(zhǎng)得一模一樣。
class father{ public void f(){}}interface sameInterface{ public void f();}class son extends father implements sameInterface{ public void f(){ //本函數(shù)實(shí)際實(shí)現(xiàn)的是父類的f(),而不是接口的f() }}注意:這種重疊在父類和接口均聲明拋出異常時(shí)可能沖突。譬如,父類聲明了異常1,接口聲明了異常2。子類實(shí)現(xiàn)成員函數(shù)時(shí),不能實(shí)現(xiàn)異常1或異常2,只能選擇不實(shí)現(xiàn)異常。
class exception1 extends Exception{}class exception2 extends Exception{}class father{ public void f()throws exception1{}}interface sameInterface{ public void f()throws exception2;}class son extends father implements sameInterface{ public void f(){ //只能選擇不實(shí)現(xiàn)異常,實(shí)現(xiàn)exception1或exception2都會(huì)報(bào)錯(cuò) }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注