The Purpose of Encapsulation


The Purpose of Encapsulation

Encapsulation is an important principle when defining classes. The idea is that a program that uses a class should not have to worry how that class actually works internally; the program simply creates an instance of a class, and calls the methods of that class. As long as those methods do what they say they will do, the program does not care how they are implemented. For example, when you call the Console.WriteLine method, you don't want to be bothered with all the intricate details of how the Console class physically arranges for data to be output to the screen. A class might need to maintain all sorts of internal state information in order to perform its various methods. This additional state information and activity is hidden from the program that is using the class. Therefore, encapsulation is sometimes referred to as information-hiding. Encapsulation actually has two purposes:

  1. To combine methods and data inside a class; in other words, to support classification.

  2. To control the accessibility of the methods and data; in other words, to control the use of the class.




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

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