next up previous contents
Next: Objects in Perl Up: Objects in Perl Previous: Polymorphism:Overriding Methods

Encapsulation:Keeping Code and Data Together

There's not much that I needs to be sid about encapsulation. Keeping the methods in the same place as the information they affect seems like common sense. It wasn't done using earlier languages mostly because the programming tools were not available. The extra work required to manually perform encapsulation outweighed the benefits that would be gained.

One big advantage of encapsulation is that it makes using information for unintended purposes more difficult, and this reduces logic errors. For example, if pens were sold in lots of 100, the changeQuantityOnHand() function would reflect this. Changing the quantity by only one would not be possible. This enforcement of business rules is one of the biggest attractions of object-oriented programming.



dave@cs.cf.ac.uk