What is encapsulation? Elaborate with example?
Answer / devarathnam.c,kotagudibanda,ka
Hi,
Encapsulation: The wrapping up of a data and methods into a
single unit is called Encapsulation. You can achieve
Encapsulation by using private data and public methods look
at the following example for better understanding.
Eg:
public class EcapTest{
public static void main(String args[]){
private int a=22;
private String s="DEVARATHNAM";
private float f=33.24f;
public void displayData(){
// some code
}
public void getData(){
// some code
}
}//main
{//class.
Observe the above program we are combining both data and
methods into a single unit(EcapTest). I hope i met your
needs.
| Is This Answer Correct ? | 23 Yes | 5 No |
What are instance variables?
How can we run a java program without making any object?
Explain about GridBag Layout?
suppose in a class there is a code like this: { Set hs=new Hashset(); hs.add(new Emp("kathy",1000)); hs.add(new Emp("kathy",2000)); } how can u avoid the above code in your class as set won't allow duplicate objects?
What is the difference between an array and an array list?
which class to use when concatenating strings in a loop.
Can you call a constructor within a constructor?
What is a singleton class in Java?
Name the method of a Container that can be used to cause a container to be laid out and redisplayed?
Difference between interface and abstract class with ex.
4 Answers Cognizant, Tech Mahindra,
Is there a case when finally will not execute?
Give any two differences between C++ and java.