Encapsulation


Encapsulation is a concept that relates to how classes are defined. It states that a class should be self-contained, meaning that it should declare all of the fields and methods to do whatever it has to do. There is one school of thought that in terprets encapsulation to mean that all access to an object's variables should be performed using methods provided by the object. Encapsulation facilitates modularity. A class that contains everything it needs can be easily incorporated into larger Java programs.

Encapsulation gives the class developer complete control over how the class is used. If the access to data is explicitly defined and controlled, it ensures that data can't be misused. Encapsulation is also a way to hide complexity. The user only has access to the publicly accessible members of a class. There may be a lot more complexity built in to the class that the user doesn't need to know about and, more importantly, shouldn't mess around with. This complexity can be hidden from outside interaction.

Encapsulation allows you to modify classes without breaking any existing applications that use the classes. As long as the public interface is unchanged, other elements of the class can be modified or added without impacting applications that use the older version. Older programs that use the class would still have to be recompiled, but their interface to the class would remain unchanged.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net