posted 23 years ago
On the other hand there is another camp that thinks there should be no reason for getters and setters or for direct access to the variables. They are just a fancier way of breaking encapsulation.
If class A is using a getter to get a variable from another class B, it intends to DO something with that value. In a perfect object oriented world, you should not have class A get the variable, you should have the class A ask for the RESULT that it wants. The class that owns the variable should be able to do ANYTHING that needs to be done with that variable.
So for example, you are asking an employee instance for it's salary using a getter
employee.getSalary();
and then using that to calculate a raise, and then use the setSalary() to put that value back.
Instead you should just send the percent of the raise to the employee and let it give itself a raise.
employee.increaseSalary(.15);
Of course there are obvious reasons why things are seldom this pure, life being what it is, but it IS a good argument to take back to your lecturer.
"JavaRanch, where the deer and the Certified play" - David O'Meara