![]() |
![]() |
![]() |
![]() |
![]() |
|
The need-to-know principle: Generally, when you design classes to be used by other programmers, your classes will contain more instance variables and methods than you expect to be accessed by the methods written by those other programmers.
By restricting access to instance variables and methods in public interfaces, you can revise and improve the other instance variables and methods without worrying about whether other programmers have already come to depend on them.
For example, when you define an Attraction class, you might choose
to make the minutes instance variable private, requiring all access
to be through public access methods. Your rationale would be that you
could change later to an hours-based definition without fear that anyone
would have come to depend on direct access to the minutes instance
variable. Instead, all users of the Attraction class would have to
use the getMinutes and setMinutes methods in the public
interface, which you easily could redefine to work with an hours
instance variable.