Java Reference
In-Depth Information
firstName = empFirstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String empLastName)
{
lastName = empLastName;
}
public String getLastName()
{
return lastName;
}
}
firstName
and
lastName
fields, which were added to improve performance. Be-
cause
setFirstName()
and
setLastName()
willbecalledmorefrequentlythan
setName()
,andbecause
getFirstName()
and
getLastName()
willbecalled
morefrequentlythan
getName()
,itismoreperformant(ineachcase)tohavethefirst
twomethodsset/get
firstName
'sand
lastName
'svaluesratherthanmergingeither
value into/extracting this value from
name
's value.
setLastName()
, and
getName()
calling
getFirstName()
and
getLastName()
, rather than directly accessing the
firstName
and
lastName
fields.Althoughavoidingdirectaccesstothesefieldsisnotnecessaryinthisexample,
imagine another implementation change that adds more code to
setFirstName()
,
setLastName()
,
getFirstName()
, and
getLastName()
; not calling these
methods will result in the new code not executing.
Client code
(code that instantiates and uses a class, such as
Employee
) will not
that shown in
Listing 2-14
, because the original interface remains intact, although the
interface has been extended. This lack of breakage results from hiding
Listing 2-13
'
s
implementation, especially the
name
field.