Friday, April 22, 2011

Encapsulation

Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions). Older languages did not enforce any property/method relationships. This often resulted in side effects where variables had their contents changed or reused in unexpected ways and spaghetti code that was difficult to unravel, understand and maintain. Encapsulation is one of three fundamental principles in object oriented programming.

Data hiding is the ability of objects to shield variables from external access. It is a useful consequence of the encapsulation principle. Those variables marked as private can only be seen or modified through the use of public accessor and mutator methods. This permits validity checking at run time. Access to other variables can be allowed but with tight control on how it is done. Methods can also be completely hidden from external use. Those that are made visible externally can only be called by using the object's front door (ie. there is no 'goto' branching concept).
A class is a template or prototype for each of many object instances made to the class design. The class specifies the properties (data) and methods (actions) that objects can work with.

No comments:

Post a Comment

Chitika