Setters

Since age does not translate uniquely into a date-of-birth, we probably wouldn't make a corresponding setter for age. But we might make one for date-of-birth, to provide for correction of that information.

 

A possible implementation of setDOB:

 

class Person
{
String name;
Date dateOfBirth;
 ....
void setDOB(Date newDateOfBirth)	// sets date-of-birth
  {
  dateOfBirth = newDateOfBirth;
  }
}

 

p.setDateOfBirth(new Date(80, 11, 30));

 

The use of a setter is preferable to just changing the dateOfBirth outright. For example, we may wish to install a log field which logs all changes to the record. Calling setDOB would add an entry to this log.

 

Next Slide Previous Slide Contents