answersLogoWhite

0


Best Answer

Java Encapsulation

Encapsulation is the concept of hiding the implementation details of a class and allowing access to the class through a public interface. For this, we need to declare the instance variables of the class as private or protected.

The client code should access only the public methods rather than accessing the data directly. Also, the methods should follow the Java Bean's naming convention of set and get.

Encapsulation makes it easy to maintain and modify code. The client code is not affected when the internal implementation of the code changes as long as the public method signatures are unchanged. For instance:

public class Employee

{

private float salary;

public float getSalary()

{

return salary;

}

public void setSalary(float salary)

{

this.salary = salary;

}

User Avatar

Wiki User

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

Wiki User

16y ago

The term "encapsulation" is usually used to point up the advantage of Object Oriented method in computer programming approach. It means that in Object-Oriented approach, an object is encapsulated from any other object there is. Everything inside an object, cannot be "touched" (accessed) by any other object within the program, unless it is permitted to be.

So whenever we want any property of an object (constants, variables, functions, or procedures) to be revealed to others, we just have to set it as a "Public" property (usually done by typing the word "Public" in the beginning of its declaration). And when we don't want it to be, we set it as "Private".

And encapsulation is an advantage because as the programming goes on, we don't have to worry about the same constant, variable, function, or procedure's name within the whole program. Every object has its own properties, and can only be accessed if it was allowed.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Encapsulation is also known as information hiding, it is to protect data from the client using the classes but still allowing the client to access the data, but not modify it. Through a public interface, the private data can be used by the client class without the worry of the user messing with the private data. An example of encapsulation is declaring an instance variable private, and having an accessor method that allow access to the variable.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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 (or at least protected) 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?

• Keep instance variables private or protected.

• Make public mutator and accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.

• For the methods, use the JavaBeans naming convention of set (for mutators) and get (for accessors).

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Encapsulation in an important concept in object-oriented programming. Some people refer to encapsulation as "information hiding" or "implementation hiding."

While good abstraction practices demand that classes group together common data and functions, good encapsulation practices demand that the details of how the class works should be hidden from users of the class.

Other programmers should know what your class does and how to use it for themselves; they should not know how it does what it does.

An example using Java

At some point we've all used the ArrayList class to store data. We know that we can add objects to the ArrayList and that the ArrayList will resize itself to store those objects, but we are unaware of how this is done. All we need to know is that we call the add(object) method and our object is now a part of the list.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The best example of encapsulation would be a Class.

A class encapsulates its features (Methods and instance variables) within itself and they can be accessed only by creating an object of the class.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A real-time example of encapsulation can be a TV. A TV is fully covered, and can be operated without touching it, as in the use of a remote.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

rushi

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Real world example for encapsulation and abstraction?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Assumptions of 2 phase commit protocol?

to implement real-world entities more and more in computer language and to protect datas we require c++.c++ is basically an object oriented programming language.there are special features which are not present in c such as data encapsulation,data abstraction,polymorphism,inheritance etc.by encapsulation ,we can bind different data types in single unit.by data abstraction ,we can protect our datas.by polymorphism ,we can overload an operator.and by inheritance,we can inherit the properties of the base class.


What is a real world example of a fjord?

A fjord is a real world example of a fjord! They exist in the real world.


What is a real world example of circumference?

The Equator is a real world example, being the circumference of the Earth.


What is the real world example?

Of what?


What is a real world example of a pentagon?

where could you find a pentagon in the real world


What is a Real world example of conservation'?

Example is too omitted to be real. Example is much more unresponsive


What is a real world example of a prime number?

2 is a real world prime number.


What is a real world example of a sample?

a example of a hendecagon is a hedecagon


Can you give me some REAL world examples?

A real world example of what? Math in general? Geometry nets? Name the math concept and it'll be easier for readers to give you a real world example.


What is a real world example?

A real-world example of poetry can be anything that focuses on a real event that happened. For example, it could be about the Twin Towers, Great Wall of China, or about the wars the USA. had to go through.


What does number line means?

A picture of a straight line that serves as abstraction for real numbers.


What is a real world example of an acid?

A real world example of an acid is a lemon (a source of citric acid) or an aspirin (a derivative of salicylic acid).