Encapsulating State and Behaviors
(a savings account instance).
Entities have attributes , such as color red, make Toyota, model Camry, and balance
twenty thousand dollars. An entity's collection of attributes is referred to as its state .
Entitiesalsohave behaviors ,suchasopencardoor,drivecar,displayfuelconsumption,
deposit, withdraw, and show account balance.
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
ing entities by declaring classes that encapsulate state and behaviors, instantiating ob-
jects withspecific state values fromthese classes torepresent specific entities, andin-
teracting with objects through their behaviors.
then introduces you to its language features for representing behaviors. Because some
state and behaviors support the class's internal architecture, and should not be visible
to those wanting to use the class, this section concludes by presenting the important
concept of information hiding.
Representing State via Fields
Java lets you represent state via fields , which are variables declared within a class's
body.Entityattributesaredescribedvia instance fields .BecauseJavaalsosupportsstate
that's associated with a class and not with an object, Java provides class fields to de-
scribe this class state.
and access class fields. After discovering how to declare read-only instance and class
fields, you review the rules for accessing fields from different contexts.