answersLogoWhite

Top Answer
User Avatar
Wiki User
Answered 2011-01-23 08:37:25

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?

001
🙏
0
🤨
0
😮
0
😂
0
User Avatar

Your Answer

Loading...

Still Have Questions?

Related Questions

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.


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.


Which encapsulation must be used to enable Ethernet II frame type on your Ethernet?

Arpa encapsulation must be used to enable Ethernet II frame type on your Ethernet.


What is de-encapsulation?

Removing something from a capsule.


What is encapsulation in c?

Encapsulation is a feature of object oriented programming languages. As such there is no concept of encapsulation in C.


Which term is used to describe the process of placing one message format into another format so that the mesage could be delivered across the appropriate medium?

* encapsulation * encapsulation


What chemicals are used for encapsulation in molecular gastronomy?

There are many options once can use in terms of chemicals chemicals for encapsulation in molecular gastronomy. The most popular is Sodium alginate, derived from algae.


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

encapsulation hdlc


What is a primary function of the trailer information added by the datalink later encapsulation?

it is used for error detection


What addresses are added to the transport layer headers during the encapsulation process and what are they used for?

port numbers


Why is encapsulation important?

Without the encapsulation, it will like exposing the private parts (members in particular) to public!!


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.


Which item is an encapsulation protocol that can be used in cisco routers when configuring subinterfaces?

The ISL protocol can be used in Cisco routers when configuring the subinterfaces.


Why Encapsulation is an important concept in the context of Object Orentation?

Encapsulation is an important concept when it comes to Object Orientation programming. This is because encapsulation restricts the access to certain components. Also, it facilitates data.


What is encapsulation in programming?

Encapsulation is a process of hiding all the internal details of an object from the outside world. end of story.


What is encapsulation?

(1) In programming, the process of combining elements to create a new entity. For example, a procedure is a type of encapsulation because it combines a series of computer instructions. Likewise, a complex data type, such as a record or class, relies on encapsulation. Object-oriented programming languages rely heavily on encapsulation to create high-level objects. Encapsulation is closely related to abstraction and information hiding. (2) In networking, same as tunneling. ANSWER: Binding of data and functions into a single unit called class is known as Encapsulation.1. Encapsulate means to enclose or to cover, in a small container like a capsule.2. Encapsulate means to explain an object or event briefly but precisely.My boss wasn't one for giving long, drawn out explanations, he had encapsulation down to an art form when he described the new product in just 3 three words.3. Encapsulation: in medical terms encapsulation means contained.During surgery the doctor found the tumour was completely encapsulated in one kidney, not throughout his patients abdomen as he feared.4. Encapsulation - In computer science, encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component (other pieces of software) only need to know what the component does, and cannot make themselves dependent ...5. Encapsulation - In the manufacture of pharmaceuticals, encapsulation refers to a range of techniques used to enclose medicines in a relatively stable shell known as a capsule, allowing them to, for example, be taken orally or be used as suppositories. ...*******************************************************Encapsulation is one of the fundamental principles of object-oriented programming.Encapsulation is a process of hiding all the internal details of an object from the outside worldEncapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are requiredEncapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the classEncapsulation gives us maintainability, flexibility and extensibility to our code.Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class.Encapsulation provides a way to protect data from accidental corruptionEncapsulation hides information within an objectEncapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methodsEncapsulation gives you the ability to validate the values before the object user change or obtain the valueEncapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients.LEarn Design Pattern,C#,ASP.NET,LINQ,Sharepoint,WCF,WPF,WWF


Which term describes set of rules that determines the formatting of messages and the process of encapsulation used to forward data?

A: protocol


What term in java is used to indicate that a class's internal state is hidden from its clients?

Encapsulation............Hiding the data from others


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 data encapsulation explain with example?

Encapsulation is the process of binding together of Data and Code. It can also be defined as the concept that an object totally seperates its interface from its implementation. The concept of Encapsulation hides the implemantation details behind its interface


What is encapsulation in odbms?

ODBMS stands for object oriented database management system. Encapsulation in ODBMS can be defined as binding of data together.


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.


What is encapsulation and how it can be implemented?

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(" "); }


What is the meaning of encapsulation?

Something that is contained within a capsule. i.e., the antibiotic is contained in the capsule. Or, astronauts are contained in the space capsule. So encapsulation is the act of putting something into a capsule.


Which term describes a specific set of rules that determines the formatting of message and the process of encapsulation used to forward data?

Protocol