answersLogoWhite

0

Why is encapsulation needed?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

Imagine that we both work for the same project and first you wrote the code for a class, and then I used your class in my program. Later on, you didn't like the way the class behaved, because some of its instance variables were being set (by me from my code) to values you hadn't anticipated. Their code brought out errors in your code. (Relax, I wont do that, dont worry.) Since, it is a Java program, so you should be able just to ship out a newer version of the class, which I could replace in my programs without changing any of my own code.

The above scenario highlights two of the promises or rather i should say benefits of Object Orientation (OO): flexibility and maintainability. But these benefits will not come automatically. You have to do something. You have to write your classes and code in a way that supports flexibility and maintainability. Just because Java supports OO concepts, it cannot write code for you. Can it?? For example, imagine if you made your class with public instance variables, and those other programmers were setting the instance variables directly, as the following code demonstrates:

public class BadExample {

public int size;

public int weight;

...

}

public class AnotherBadExample {

public static void main (String [] args) {

BadExample b = new BadExample ();

b.size = -5; // Legal but bad!!

}

}

Now go back the scenario we spoke about a paragraph ago. BadExample is your class and AnotherBadExample is my code. I have modified one of your variables in a way that it helps my code logic but that totally alters the way your class works. Now you are in trouble. How are you going to change your class in such a way that no one can alter your values directly (like what i have done in my code)? Your only choice is to write a method say setSize(int newVal) inside your class and then change the access modifier of the variable size to say, private. This will ensure that you handle instances when someone is trying to set a value to the size variable that you dont want and at the same time ensure that no one can access the size variable directly and mess with your code.

But, unfortunately, by doing that, you have broken my code. If I try to compile my AnotherBadExample class, i will get errors because the size variable is no longer visible for me.

How can we address this situation now? The best way is: not write such code where public variables are available for anyone and everyone to modify.

The ability to make changes in your code without breaking the code of all others who use your code is a key benefit of encapsulation. You should always hide implementation details. To elaborate, you must always have your variables as private and then have a set of public methods that others can use to access your variables. Since the methods are public anyone can access them, but since they are in your class you can ensure that the code works the way that is best for you. So in a situation that you want to alter your code, all you have to do is modify your methods. No one gets hurt because i am just using your method names in my code and the code inside your method doesnt bother me much.

If you want maintainability, flexibility, and extensibility (and I guess, you do), your design must include encapsulation. How do you do that?

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

wrapping of data into a singal unit it call encapsulation....

what ever we write between a class its call encapsulation...

for e.x.

class s

{

int a,b,c;

float m;

char n;

printf(" ");

scanf(" ");

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The ability to make changes in your code without breaking the code of all others who use your code is a key benefit of encapsulation. You should always hide implementation details. To elaborate, you must always have your variables as private and then have a set of public methods that others can use to access your variables. Since the methods are public anyone can access them, but since they are in your class you can ensure that the code works the way that is best for you. So in a situation that you want to alter your code, all you have to do is modify your methods. No one gets hurt because i am just using your method names in my code and the code inside your method doesnt bother me much.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why is encapsulation needed?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why encapsulation is needed in java?

Encapsulation is not needed in Java, or any other language. But it's useful. Encapsulation is used to minimise the number of potential dependencies with the highest probability of change propagation.


What is the name of the process of building a frame around network layer information?

Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.Encapsulation. Actually, this is one type of encapsulation; encapsulation occurs at several layers.


Which command sets the encapsulation on a Serial interface back to the Cisco default?

encapsulation hdlc


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


Is encapsulation a characteristic of procedural or object oriented programming?

Encapsulation is one of the four pillars of object-oriented programming. The other three are inheritance, polymorphism and abstraction.


The three basic principles of object-oriented design?

Encapsulation,Inheritance and Polimorphisim...


If two switches each support all types of trunk encapsulation on a link between them which type of encapsulation will be negotiated?

12


What is mean by encapsulation of data?

In general, encapsulation is the inclusion of one thing within another thing so that the included thing is not apparent.


A user sends an HTTP request to a web server on a remote network During encapsulation for this request what information is added to the address field of a frame to indicate the destination?

When a user sends an HTTP request to a web server on a remote network, the information needed to perform the action is added to the address field of a frame to indicate the destination during encapsulation.


What network component handles encapsulation?

Protocol


What is de-encapsulation?

Removing something from a capsule.


What is pdu encapsulation?

protocol data unit