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


Please Help Members By Posting Answers For Below Questions

What is protected access modifier?

557


How do I enable java in safari?

534


What are the differences between graph and tree?

634


Is empty string in java?

522


What sorting algorithm does javascript use?

520






What is unicode used for?

562


What is data and its types?

563


In case of inheritance what is the execution order of constructor and destructor?

626


What is vector capacity in java?

589


What is the indent key?

577


How many bytes is a char in java?

518


what is synchronization? : Java thread

548


What about main() method in java ?

558


How do you implement polymorphism in our day to day life?

2753


Is it safe to install java on my computer?

526