Encapsulation


Encapsulation is an object-oriented concept that says a class should be self-contained, that it should define (or inherit) all the fields and methods it needs to do whatever it is it has to do. The flip side is that access to any field declared in a class should only be allowed through methods declared by the class. The reason is one of developer control. If you allow users unlimited access to your fields, they may do things that would change the nature of a program in a detrimental way such as changing the value of a variable that was intended to be read-only.

Encapsulation is fairly easy to put into practice. Simply give your fields private access. That way no one can access them directly outside of the class. Then define appropriate public methods to access and/or change the values of the private variables . You now have complete control over how the data in your class will be used.



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