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 internal variable?

0 Answers  


what is method reference in java 8?

0 Answers  


What is math exp in java?

0 Answers  


What are the advantages of encapsulation in java?

0 Answers  


How to change the priority of thread or how to set the priority of thread?

0 Answers  






Why does java not allow multiple public classes in a java file ?

2 Answers  


explain the concept of virtual method invocation in polymorphism in detail?

0 Answers   TCS,


what is a transient variable?

5 Answers  


Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? How can a hash map syncronized with out using syncrozed blocks in programm?

3 Answers   Four soft,


What is the USE of Null interfaces ??...if thers nothing inside these interfaces how are they used and WHy are they used ???? No 1 has given a proper description yet

1 Answers  


What is functional interface in java example?

0 Answers  


What kind of variables a class can consist of?

0 Answers  


Categories