「Java」「abstract」 Chapter4 Question1
Given:
What two changes, made independently,will enable the code to compile?
以下哪兩個選項能獨自變更並完成編譯?
abstract class Account{ abstract void deposit(double atm); public abstract boolean withdraw(double atm); } class SubAccount extends Account{ // @Override // void deposit(double atm) { // } // // @Override // public boolean withdraw(double atm) { // return false; // } }
What two changes, made independently,will enable the code to compile?
- Change the signature of Account to: public class Account.
- Change the signature of SubAccount to:public abstract class SubAccount.
- Implement private methods for deposit and withdraw in SubAccount.
- Implement public methods for deposit and withdraw in SubAccount.
- Change Signature of SubAccount to: SubAccount implements Account.
- Make Account an interface.
以下哪兩個選項能獨自變更並完成編譯?
- 更改Account簽章成: public class Account (錯誤,因Account裡有abstract方法)
- 更改SubAccount簽章成: public abstract class SubAccount(正確,因子類別SubAccount改成abstract,也就不需要覆寫方法)
- 實作 修飾子為private 的 deposit 與 withdraw 方法(錯誤,因Account最高為default,而private權限更高於default。子類別的覆寫方法權限不可高於父類別 )
- 實作 修飾子為public 的 deposit 與 withdraw 方法(正確,public權限高於覆類別 deposit 與 withdraw方法。)
- 更改SubAccount簽章成: SubAccount implements Account(錯誤,Account不是interface)
- 更改 Account 為 interface 介面。 (錯誤,且毫無異議)
留言
張貼留言