book
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Private Account<br />
1 package bankaccount;<br />
2<br />
3 class Account {<br />
4 private int accountNo;<br />
5 private double balance;<br />
6 private String accountName;<br />
7<br />
8 Account(int accountNo, String accountName, double balanc<br />
e) {<br />
9 this.accountNo = accountNo;<br />
10 this.accountName = accountName;<br />
11 this.balance = balance;<br />
12 }<br />
13<br />
14 public double withdraw(double amount) {<br />
15 if (balanceCleared(amount)) {<br />
16 balance = balance - amount;<br />
17 } else {<br />
18 System.out.println(''Insufficient Funds");<br />
19 }<br />
20 return balance;<br />
21 }<br />
22<br />
23 private boolean balanceCleared(double amount) {<br />
24 if (balance - amount >= 0) {<br />
25 return true;<br />
26 } else {<br />
27 return false;<br />
28 }<br />
29 }<br />
30 }<br />
Private Account<br />
Consider the following statements issued from another class in the bankaccount package:<br />
Account fredsAccount = new Account(123, "Fred", 60);<br />
if (fredsAccount.balanceCleared(20) ) System.out.println("OK");<br />
fredsAccount.accountName = "FRED";<br />
The first statement is legal because the Account constructor has the package access<br />
level by default. The second statement is illegal because the balanceCleared method in<br />
the Account class has private access level. The third statement is also illegal because<br />
the accountName variable is private. A program containing the second and third<br />
statements will not compile.