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.
Joined: Aug 03, 2003
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.
Joined: Jul 11, 2001
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?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus