twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Setting Private Variables
|
Posted: Mar 13, 2004 7:38 PM
|
|
As you mentioned, validating data is a one reason - a big one in my mind.
Another is the situation where the value of one field may affect the value of others fields. For example, changing the "energy" level of a video game character might also affect its strength, speed or even size.
Perhaps the most important reason is that keeping variables private also prevents them from accidentally being overridden when a class is subclassed.
It is easy to overlook that when you are just learning since you typically are the only one using your code. But in a "real world" situation, it is common to be working with classes created by someone else. If a variable inadvertantly gets overridden by somebody that is subclassing it, the problems that result can be difficult to track down.
twc
|
|