Java Reference
In-Depth Information
System.out.println("Invalid Account Opening");
} catch (IllegalDeposit d){
System.out.println("Invalid Deposit");
} catch (IllegalWithdrawal d){
System.out.println("Invalid Withdrawal");
}
}
}
How It Works
Here's how it works:
1.
The Accountable interface prescribes the types of behavior an Accountable class should include,
namely deposit and withdraw. Some new exception types were defined to differentiate between the
problems that may occur. The commenting describes the purpose of each method and any param-
eters or exceptions.
2.
The SavingsAccount class implements the methods laid out in the Accountable interface. It also
includes getters and setters for the instance variables and a toString() method for printing.
3.
The CheckingAccount class is very similar to the SavingsAccount class, with a few small changes
to account for the different rules attached to checking accounts, the minimum balance in particu-
lar. It is easy to see how these classes could easily be merged with a minimum balance of 0 for
savings accounts. However, if more differences are expected or more types of accounts might be
added, an interface with implementing classes may be appropriate.
4.
The AccountManager class was organized differently than the previous exercise simply to show an
alternate approach. Here the entire method is inside a try block, with three catch blocks to alert
the user to problems with account creation, depositing, or withdrawing.
garbage collection
Garbage collection is a way to reclaim memory from objects once they are no longer in use. You saw
in Chapter 6 the OutOfMemoryError that's thrown when there is insufficient heap space for creat-
ing a new object. By removing unused objects from memory, more is available for new and existing
objects. One difference between C++ and Java is that Java incorporates automatic garbage collec-
tion, so the programmer does not need to manage memory as much themselves.
An object is eligible for garbage collection when it is no longer accessible through any variable.
However, if one object refers to another and that object in turn refers to the first in a cyclical depen-
dency, they may both be eligible for garbage collection. Once an object is eligible for garbage collec-
tion, the garbage collector will eventually remove it from memory.
Recall how when a new object is created, the constructor method is called to initialize it. There is
also a finalize method that's invoked just before the object is destroyed. This method is built in to
the Object superclass, but you can override it in your classes to perform special actions before an
object is destroyed.
 
Search WWH ::




Custom Search