Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of encapsulation

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encapsulation means wrapping up of member data and member functions together and this is a feature of OOP.
But,what it the benefit we are getting by using encapsulation or in other words what we will be loosing if not using encapsulation.
thanks in advance.
 
sammaiah kyatham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
There are several advantages of encapsulation.
The primary advantage is data access and managing data for a specific entity/Object.
Encapsulation allows you to bind the data within the object and provides set of operations to manipulate the data.

I'm not sure whether you are happy with my answer but you'll understand practically if you write one C program and java program for same requirement like managing employee data.

Thanks,
Sam
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main advantage of encapsulation is that it keeps your code maintainable. By keeping things that are closely coupled also close together in the source code, it becomes easier to assess the impact of changes to the code.

For example, imagine that a value that until now has been stored in a field should now be calculated one the fly from the value of another field. That's easy to do if the field is only directly accessed from inside the class and all other clients go through methods. Without encapsulation, it can even become impossible - especially when not all clients are under your direct control.

Does that help?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic