This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Encapsulation example  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys i want to know why we are using encapsulation?
can any one give me an industrial sample for the encapsulation?
 
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhanushanth Dhanu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir it's really wonder full..............thanks a lot for your consideration.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!