Java Reference
In-Depth Information
terfaceextension.Becauseinterfacesareinvolved,Javareferstothiskindofinheritance
as interface inheritance.
Some real-world entities can change their forms. For example, water is naturally a
liquid, but it changes to a solid when frozen, and it changes to a gas when heated to
itsboilingpoint.Insectssuchasbutterfliesthatundergometamorphosisareanotherex-
ample.
The ability to change form is known as polymorphism, and is useful to model in a
programminglanguage.Forexample,codethatdrawsarbitraryshapescanbeexpressed
more concisely by introducing a single Shape class and its draw() method, and
by invoking that method for each Circle instance, Rectangle instance, and other
Shape instance stored in an array. When Shape 's draw() method is called for an
arrayinstance,itisthe Circle 's, Rectangle 'sorother Shape instance's draw()
methodthatgetscalled.Wesaythattherearemanyformsof Shape 's draw() method,
or that this method is polymorphic.
Every class X exposes an interface (a protocol consisting of constructors, methods,
and[possibly]fieldsthataremadeavailabletoobjectscreatedfromotherclassesforuse
increatingandcommunicatingwith X 'sobjects).Javaformalizestheinterfaceconcept
byprovidingreservedword interface ,whichisusedtointroduceatypewithoutim-
plementation.Althoughmanybelievethatthislanguagefeaturewascreatedasawork-
aroundtoJava'slackofsupportformultipleimplementationinheritance,thisisnotthe
real reason for its existence. Instead, Java's interfaces feature was created to give de-
velopers the utmost flexibility in designing their applications, by decoupling interface
from implementation.
Objects are created via reserved word new , but how are they destroyed? Without
somewaytodestroyobjects,theywilleventuallyfilluptheheap'savailablespaceand
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
agarbagecollector,whichiscodethatrunsinthebackgroundandoccasionallychecks
forunreferencedobjects.Whenthegarbagecollectordiscoversanunreferencedobject
(ormultipleobjectsthatreferenceeachother,andwheretherearenootherreferencesto
eachother—onlyAreferencesBandonlyBreferencesA,forexample),itremovesthe
object from the heap, making more heap space available.
Nowthat youunderstand Java'ssupportforclasses andobjects, you'rereadytoex-
plorethislanguage'ssupportformoreadvancedfeaturessuchaspackagesandgenerics.
Chapter3 introducesyoutoJava'ssupportfortheseandotheradvancedlanguagefea-
tures.
Search WWH ::




Custom Search