how the data is hidden and safe if encapsulation is implemented?
Answers
Answered by
23
!!•••••••☆☆ ☆☆•••••••!!
There is data hiding. The second example hides how the value is stored. Also, the second example makes the value read-only for code outside the class. By having private fields and exposing them through methods or properties you immediately gain some advantages, some of which are:
•Access control granularity; you can choose to make the value publicly read-only, read-write or write-only.
•Possibility to validate input prior to storing the value
•Possibility to add logging or other auditing mechanisms
Note that encapsulation is not only about having access control. The primary use for encapsulation is to hide implementation details from calling code. When calling code wants to retrieve a value, it should not depend on from wherethe value comes. Internally, the class can store the value in a field or retrieve it from some external resource (such as a file or a database). Perhaps the value is not stored at all, but calculated on-the-fly. This should not matter to the calling code.
When you expose the value through a method or property, you shield calling code from such details, which also gives you the possibility to change the implementation, without affecting calling code.
☆☆☆☆• Hope Help u •☆☆☆☆
There is data hiding. The second example hides how the value is stored. Also, the second example makes the value read-only for code outside the class. By having private fields and exposing them through methods or properties you immediately gain some advantages, some of which are:
•Access control granularity; you can choose to make the value publicly read-only, read-write or write-only.
•Possibility to validate input prior to storing the value
•Possibility to add logging or other auditing mechanisms
Note that encapsulation is not only about having access control. The primary use for encapsulation is to hide implementation details from calling code. When calling code wants to retrieve a value, it should not depend on from wherethe value comes. Internally, the class can store the value in a field or retrieve it from some external resource (such as a file or a database). Perhaps the value is not stored at all, but calculated on-the-fly. This should not matter to the calling code.
When you expose the value through a method or property, you shield calling code from such details, which also gives you the possibility to change the implementation, without affecting calling code.
☆☆☆☆• Hope Help u •☆☆☆☆
Answered by
33
Explanation:
This technique is known as encapsulation because it seals the data (and internal methods) safely inside the "capsule" of the class, where it can be accessed only by trusted users (i.e., by the methods of the class). ... The most important reason is to hide the internal implementation details of your class.
Similar questions
Physics,
7 months ago
English,
7 months ago
Social Sciences,
7 months ago
Math,
1 year ago
Physics,
1 year ago