Java Reference
In-Depth Information
Abstract classes support implementation, but can be genuinely abstract ( Listing
2-37 ' sabstract Shape class,forexample).However,theyarelimitedtoappearingin
the upper levels of class hierarchies.
Interfaces and abstract classes can be used together. For example, the Collections
Framework's java.util package provides List , Map , and Set interfaces; and
AbstractList , AbstractMap , and AbstractSet abstract classes that
provide skeletal implementations of these interfaces.
Theskeletalimplementations makeiteasyforyoutocreateyourowninterfaceim-
plementations,toaddressyouruniquerequirements.Iftheydonotmeetyourneeds,
you can optionally have your class directly implement the appropriate interface.
Collecting Garbage
Objectsarecreatedviareservedword new ,buthowaretheydestroyed?Withoutsome
way to destroy objects, they will eventually fill up the heap's available space and the
application will not be able to continue. Java does not provide the developer with the
ability to remove them from memory. Instead, Java handles this task by providing a
garbage collector , which is code that runs in the background and occasionally checks
forunreferencedobjects.Whenthegarbagecollectordiscoversanunreferencedobject
(ormultipleobjectsthatreferenceeachother,andwheretherearenootherreferencesto
eachother—onlyAreferencesBandonlyBreferencesA,forexample),itremovesthe
object from the heap, making more heap space available.
An unreferenced object isanobjectthatcannotbeaccessedfromanywherewithinan
application.Forexample, new Employee("John", "Doe"); isanunreferenced
objectbecausethe Employee referencereturnedby new isthrownaway.Incontrast,
a referenced object isanobjectwheretheapplicationstoresatleastonereference.For
example, Employee emp = new Employee("John", "Doe"); isareferenced
object because variable emp contains a reference to the Employee object.
A referenced object becomes unreferenced when the application removes its last
storedreference.Forexample,if emp isalocalvariablethatcontainstheonlyreference
toan Employee object, thisobject becomes unreferenced whenthemethod inwhich
emp isdeclaredreturns.Anapplicationcanalsoremoveastoredreferencebyassigning
null toitsreferencevariable.Forexample, emp = null; removesthereferenceto
the Employee object that was previously stored in emp .
Java's garbage collector eliminates a form of memory leakage in C++ implementa-
tions that do not rely on a garbage collector. In these C++ implementations, the deve-
lopermustdestroydynamicallycreatedobjectsbeforetheygooutofscope.Iftheyvan-
Search WWH ::




Custom Search