What is encapsulation? Elaborate with example?
Answer Posted / 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 |
Post New Answer View All Answers
What is a class reference?
What is java in simple terms?
What does \ mean in regex?
Name few "optional" classes introduced with java 8 ?
What is parsing a string?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
Can you change array size in java?
Difference between process and thread?
What are the methods of object class ?
Give any two differences between C++ and java.
What is java lang string?
Does .length start 0 java?
Write a java program to print fibonacci series?
What is the default value of an object reference declared as an instance variable?
What is definition and declaration?