0

Possible Duplicate:
Considering object encapsulation, should getters return an immutable property?

Does encapsulation mandate immutability of the class?

Class Employee{
  private Date hireDate;
  public Date getHireDate(){
   return hireDate;
  }
}

In Some client Method:

Employee emp = new Employee();
Date temp = emp.getHireDate();
temp.setTime(...);//The Hiredate of the employee would be corrupted...
Community
  • 1
  • 1
hakish
  • 3,990
  • 7
  • 39
  • 56

1 Answers1

0

No. It just means that access to the class's members is only permitted via its methods.

If you want to prevent the kind of problem you highlight, the class could return a copy of the Date object. But even then, you could provide a setHireDate() method and still rightly refer to the date as "encapsulated".

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365