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

Can we extend singleton class?

535


What are the two main uses of volatile in Java?

607


What is meant by oops concept in java?

508


How do you create a bulleted list?

569


What is empty string literal in java?

567






What are the types of arrays in java?

563


What is java full form?

534


What is functional interface in java example?

537


What about main() method in java ?

568


Where is java located?

501


How is java created?

541


Is string passed by reference in java?

551


What is meant by structural programming?

555


What is Java Annotations?

558


How can we access some class in another class in java?

555