Difference between revisions of "Encapsulation"
Line 1: | Line 1: | ||
− | Encapsulation is concept of packaging together all of the data items and methods from an object. The data values within an object or class would separated from the methods. This is done with key words such as "public", "private" and "protected". | + | Encapsulation is concept of packaging together all of the data items and methods from an object. The data values within an object or class would separated from the methods. This is done with key words such as "public", "private" and "protected". Data items will normally be private to protect them, and the class or object will have public methods which can access or assign a value to the private data item.This prevents a direct link between the two. |
<h2>Public</h2> | <h2>Public</h2> | ||
Line 9: | Line 9: | ||
<h2>Protected</h2> | <h2>Protected</h2> | ||
The "protected" modifier means that only the class in which data is declared and its subclasses can see the data. | The "protected" modifier means that only the class in which data is declared and its subclasses can see the data. | ||
− | |||
− |
Revision as of 21:27, 16 December 2016
Encapsulation is concept of packaging together all of the data items and methods from an object. The data values within an object or class would separated from the methods. This is done with key words such as "public", "private" and "protected". Data items will normally be private to protect them, and the class or object will have public methods which can access or assign a value to the private data item.This prevents a direct link between the two.
Public
The "public" modifier allows data to be viewed and manipulated by any part of the program.
Private
The "private" modifier means that only the class in which data is declared can it be viewed.
Protected
The "protected" modifier means that only the class in which data is declared and its subclasses can see the data.