Java Reference
In-Depth Information
{
// Create objects of the class Pixel
Pixel pix1 = new Pixel(10, 50);
Pixel pix2 = new Pixel(90, 200);
// Since the method midPix is static, it is called with
// a class reference
Pixel pix3 = Pixel.midPix(pix1, pix2);
// Display location of all three Pixel objects
pix1.pixLocation();
pix2.pixLocation();
pix3.pixLocation();
}
}
Acquaintance Associations
Westartedthisdiscussionconsideringthecaseofaclassthatcontainsan
objectofanotherclass,whichiscalledanaggregationrelationship.How-
ever,intheprogramPixelOps.java,weseeaclassthatreceivesasaparame-
tertheobjectthatitusestoaccessthemethodsofanotherone.Sinceinthis
casetheobjectisnotcontainedintheclass,wespeakofacaseofobject
compositionby acquaintance .Intheacquaintancerelationshipthebinding
islooserthaninobjectcomposition,sinceinthecaseofacquaintancethe
objectmaynotbedefineduntilruntime.
Incidentally...
Itisthislooserbindingthatmakesitpossibletouseacquaintanceto
implementdynamicbinding.
Supposeanapplicationhasaccesstotwomethodstocalculateareas.
BothmethodsarenamedArea().OneArea()method,locatedinaclass
namedRectangle,calculatestheareaofarectangularfigure.Another
Area()methodislocatedinaclassnamedCircleandcalculatesthearea
ofacircularfigure.NowsupposethatwearecodingaclassnamedWin-
dow,whichcanbecircularorrectangularinshape,andwewishtouse
thearea-calculatingmethodsintheclassesRectangleandCircle.One
possibleapproachistocodetwomethodsnamedWinArea()intheclass
Window.OnemethodreceivesaRectangleobjectasaparameterandthe
otheroneaCircleobject.SincetheWinArea()methodshavedifferentsig-
naturestheyarenotpolymorphic.Whichmethodisuseddependsonthe
objectreceivedasaparameter.Theclassdiagramin Figure17-3 shows
the class structure in this example.
Search WWH ::




Custom Search