Forums Register Login

Encapsulation example

+Pie Number of slices to send: Send
hi guys i want to know why we are using encapsulation?
can any one give me an industrial sample for the encapsulation?
+Pie Number of slices to send: Send
Encapsulation is one of the four fundamental OOP concepts.
The ability to make changes in your implementation code witout breaking the code of others who use your code is a key benefit of encapsulation.

Let me give you an example:
Lets say you have a class User, which holds User name and password;


And alot of other programmers(may be 1000s) wrote programs that used your class such as:

Now your Manager announced some new rules:
1. user name cannot be null.
2. password cannot be null.
3. No one can set the user name less then 5 characters.
4. No one can set the password less then 5 characters.

Just Think how easy it would be to make above changes by following the encapsulation rules. instead of changing all the code where your User class is used you would change only setUserName(string) & setPassword(string) method as per code listed below.
You made the changes only at one place and the the other 1000s of programmers code will adopt those rules automatically!!



Hope This Helps.
+Pie Number of slices to send: Send
thank you sir it's really wonder full..............thanks a lot for your consideration.
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 804 times.
Similar Threads
Encapsulation
encapsulation
Where needed javax.servlet.jsp.tagext
Struts simple application with eclipse
How to store the information in SQL server with XML file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:06:12.