3 ms·
The code does not demonstrate encapsulation because the data type has not been decoupled from the implementation. A developer cannot change the internal represe
by thangalin 13y ago
The code does not demonstrate encapsulation because the data type has not been decoupled from the implementation. A developer cannot change the internal representation to a float without changing code that calls ReturnResult, thereby defeating encapsulation. (Java cannot have two methods of the same name that differ by return type alone.)
Encapsulation means that objects should perform operations on their data, instead of giving their data to other objects. See Tell, Don't Ask (http://pragprog.com/articles/tell-dont-ask http://pragprog.com/articles/tell-dont-ask) for details. Encapsulation also lends itself to the Open-Closed Principle whereby class behaviours can be changed through inheritance alone.
Please see (and critique) my self-encapsulation presentation for details:
http://whitemagicsoftware.com/encapsulation.pdf http://whitemagicsoftware.com/encapsulation.pdf
- adwn 13y ago> The code does not demonstrate encapsulation because the data type has not been decoupled from the implementation. Not all code has to be applicable to more than one use case. What you're suggesting is premature generalization, which is just as bad as premature optimization. One might argue that p.g. is a kind of p.o. with the metric to be optimized being abstraction.