Java Reference
In-Depth Information
cedures that manipulate them. Although Java lets you create applications in a similar
fashion, this language is really about declaring classes and creating objects from these
classes.
Aclassisatemplateformanufacturingobjects(namedaggregatesofcodeanddata),
whicharealsoknownasclassinstances,orinstancesforshort.Classesgeneralizereal-
world entities, and objects are specific manifestations of these entities at the program
level.
Classesmodelreal-worldentitiesfromatemplateperspective.Objectsrepresentspe-
cificentities.Entitieshaveattributes.Anentity'scollectionofattributesisreferredtoas
its state. Entities also have behaviors.
Aclassanditsobjectsmodelanentitybycombiningstatewithbehaviorsintoasingle
unit—the class abstracts state whereas its objects provide concrete state values. This
bringing together of state and behaviors is known as encapsulation. Unlike structured
programming, where the developer focuses on modeling behaviors through structured
code,andmodelingstatethroughdatastructuresthatstoredataitemsforthestructured
codetomanipulate,thedeveloperworkingwithclassesandobjectsfocusesontemplat-
ingentitiesbydeclaringclassesthatencapsulatestateandbehaviorsexpressedasfields
and methods, instantiating objects with specific field values from these classes to rep-
resent specific entities, and interacting with objects by invoking their methods.
We tend to categorize stuff by saying things like “cars are vehicles” or “savings ac-
counts are bank accounts.” By making these statements, we really are saying that cars
inherit vehicular state (such as make and color) and behaviors (such as park and dis-
playmileage),andsimilarlyaresayingthatsavingsaccountsinheritbankaccountstate
(such as balance) and behaviors (such as deposit and withdraw). Car, vehicle, savings
account,andbankaccountareexamplesofreal-worldentitycategories,andinheritance
isahierarchicalrelationshipbetweensimilarentitycategoriesinwhichonecategoryin-
heritsstateandbehaviorsfromatleastoneotherentitycategory.Inheritingfromasingle
categoryiscalledsingleinheritance,andinheritingfromatleasttwocategoriesiscalled
multiple inheritance.
Java supports single inheritance and multiple inheritance to facilitate code re-
use—why reinvent the wheel? Java supports single inheritance in a class context, in
which a class inherits fields and methods from another class through class extension.
Becauseclassesareinvolved,Javareferstothiskindofinheritanceasimplementation
inheritance.
Javasupportsmultipleinheritanceonlyinaninterfacecontext,inwhichaclassinher-
itsmethodtemplatesfromoneormoreinterfacesthroughinterfaceimplementation,or
inwhichaninterfaceinheritsmethodtemplatesfromoneormoreinterfacesthroughin-
Search WWH ::




Custom Search