Java Reference
In-Depth Information
firstName = empFirstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String empLastName)
{
lastName = empLastName;
}
public String getLastName()
{
return lastName;
}
}
Listing 2-14 reveals that the name field has been removed in favor of new
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.
Listing 2-14 also reveals setName() calling setFirstName() and
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
break when Employee 's implementation changes from that shown in Listing 2-13 to
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.
Search WWH ::




Custom Search