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

explain local datetime api in java8?

0 Answers  


What is difference between variable declaration and definition?

0 Answers  


How do you start a new line in java?

0 Answers  


How do you check if a number is a perfect square?

0 Answers  


Scenario: There are 1 to 100 numbers. Each number should be keep in the each column like from A column to Z column ie 1 to 26. From 27 to 52 should be in 2nd row in the excel sheet. This has to be continue till 100. How do you write Java program and what are various methods.

4 Answers   Cap Gemini,






Program to output as below formate: 1 2 3 4 5 6 7 8 9 10

4 Answers   Huawei,


perpare on factorypattern,linklist wothout using collection, (multitharding ie create producer/customer therad producer create Queue continuesly,consumer consume queue, consumer wait if queue is full,producer wait if queue is empty),diff betn sleep(1000)&wait(1000) these r the main Q ask in huawei(2008)

1 Answers   Huawei,


What is console based application in java?

0 Answers  


What is java objectoutputstream?

0 Answers  


What does += mean in java?

0 Answers  


Write an algorithm program in java for the following question.. 1) S is a set of integers.X is an integer obtained by sum of two digits in S. Write logic for whether or not the X is from the S. The time of algorithm should not exceed o(n logn).

0 Answers  


How does abstract modifier work?

1 Answers   Wipro,


Categories