Java Reference
In-Depth Information
Implementing Interfaces
try it out
To compare and contrast with superclasses, this exercise demonstrates the implementation of interfaces
using the Bank Account examples from earlier.
1.
You may want to create a new package to keep your bank account classes separated from other
account classes you've been working on already.
2.
Create a new interface the same way you do a class, but by selecting New Interface instead of
New Class. Name it Accountable .
3.
Add four methods to the Accountable interface:
boolean isValidDeposit();
void deposit();
boolean isValidWithdrawal();
void withdraw();
4.
Write meaningful comments for the interface and each method. If you type /** and press Enter, the
multiple‐line comment block will be generated for you. Your Accountable interface may look like
this:
/**
* an interface to manage accounting behavior
*/
public interface Accountable {
 
/**
* Returns true if a specified String is a valid amount to deposit.
* @param deposit: the amount to be deposited
*/
boolean isValidDeposit(String deposit);
 
/**
* Adds the specified amount to the balance.
* @param deposit: the amount to be deposited
* @throws IllegalDeposit
*/
void deposit(String deposit) throws IllegalDeposit;
 
/**
* Returns true if a specified String is a valid amount to withdraw.
* @param withdrawal: the amount to be withdrawn
*/
boolean isValidWithdrawal(String withdrawal);
 
/**
* Subtracts the specified amount from the balance.
* @param withdrawal: the amount to be withdrawn
* @throws IllegalWithdrawal
*/
void withdraw(String withdrawal) throws IllegalWithdrawal;
}
Search WWH ::




Custom Search