What is meant by Encapsulation? Can you write a class to
explain encapsulation?

Answers were Sorted based on User's Feedback



What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / sivakishorereddy(badvel)

-Encapsulation is wrapping of data(Data members) and
associated methods(member functions) into a single unit in
such a way that data can be accessed with the help of
associated methods.
--In a class we can specify the variables(Data members) as
private. So for other classes it will not be accessible.
--Through methods(member functions) only we can access the
member variables or modify them.
--It gives more security for our data. It is nothing but
data hiding.

Is This Answer Correct ?    19 Yes 1 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / bln

Encapsulation is process binding data with an object.
Object consists data and methods, where methods helps to
extract or set data.

All classes developed using OOPS concepts follow the
encapsulation.

Is This Answer Correct ?    15 Yes 4 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / rajasekhar

It is the ability of an object to hide its data and methods
from the rest of the world.

A Class may contains a few members in the form of
properties,events,fields or methods.These members of the
class are not exposed to the outer world directly.Rather
they are ENCAPSULATED by the Class.

Public Class Calculations
{
Private void fnMultiply(int x, int y)
{
return x*y;
}
}

Calculations Obj;
int Result;
Result = Obj.fnMultiply(5,10);

Is This Answer Correct ?    19 Yes 10 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / srinu

Encapsulation=DataHidding+Abstraction is called
Encapsulation is called Encapsulation

EX:-
public class A
{
private String name;
private int age;
publiic setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public setAge(int age)
{
this.age=age;
}
public int age()
{
return age;
}
}

Is This Answer Correct ?    6 Yes 2 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / srinivasa

All the javabeans written in java follows encapsulation.
In java bean variables are declared as private for
security. will be accessed using setter and getter method.

Is This Answer Correct ?    4 Yes 2 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / anjani kumar jha


Binding the Function with its associated data together is
called encapsulation(Tough definition but here is example also)

Think this senario
public class Anjani{
public int size;
public int weight;
...
}
public class Jha {
public static void main (String [] args) {
Anjani a = new Anjani();
a.size = -5; // Legal but bad!!
}
}

if somebody change ur size from -5 to -10; so sad ..........
And now you're in trouble. How are you going to change the
class in a way that
lets you handle the issues that come up when somebody
changes the size variable
to a value that causes problems? Your only choice is to go
back in and write method
code for adjusting size (a setSize(int a) method, for
example), and then
protect the size variable with, say, a private access
modifier. But as soon as you
make that change to your code, you break everyone else's!

The ability to make changes in your implementation code
without breaking the
code of others who use your code is a key benefit of
encapsulation.

Now how u will do this............

Keep instance variables protected (with an access modifier,
often private).
and Make public accessor methods, and force calling code to
use those methods
rather than directly accessing the instance variable.
and For the methods, use the JavaBeans naming convention of
set<someProperty> and get<someProperty>.


so how u will write the code........

full code of incapsulation

class Anjani{
private int size;
public int setSize(int size)
{
this.size=size;

return size;

}
public int getSize()
{
this.size=size;
return size;
}

}
public class jha {
public static void main (String [] args) {
Anjani a = new Anjani();
int x=a.getSize();
a.setSize(10);
System.out.println(a.setSize(10));
}
}

Thanks and Regards
Anjani Kumar Jha
09623154095
CDAC PUNE

Is This Answer Correct ?    3 Yes 5 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / vinoth

hai thanks for giving excellent answers...

Is This Answer Correct ?    1 Yes 5 No

What is meant by Encapsulation? Can you write a class to explain encapsulation?..

Answer / ajay kumar sharma

Wraping of objects under a class is known as Encapsulation.

class
{
public static void main(String a[])
{
int a,b,
....
....
}
}

Is This Answer Correct ?    2 Yes 9 No

Post New Answer

More Core Java Interview Questions

What is complexity in java?

0 Answers  


what is the use of bean managed and container managed with example?

0 Answers   EDS,


What is the difference between variable & constant?

0 Answers  


Can a string be null?

0 Answers  


Which class is the superclass for every class in java programming?

0 Answers  






What is boolean in java?

0 Answers  


I want to store more than 10 objects in a remote server? Which methodology will follow?

0 Answers  


What is basic concept of static in java ?

1 Answers   iGate,


What are the problems faced by java programmers who don't use layout managers?

0 Answers  


Why method overriding is used?

0 Answers  


what is multi-tasking

3 Answers   Anand Group, Photon Infotech, Tech Mahindra,


What is garbage collection in Java, and how can it be used ?

32 Answers   Accenture, HCL, Infosys, Sara, SITS, TCS, Wipro,


Categories