freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Encapsulation:-

The process of binding the data and code as a single unit is called encapsulation.
We are able to provide more encapusulation by taking the private data(variables) members.
To get and set the values from private members use getters and setters to set the data and to get the data.

Java Images

Encapsulation program

class Encapsulation
{
private int sid;
private int sname;
public void setSid(int x)
{
this.sid=sid;
}
public int getSid()
{
return sid;
}
public void setSname(String sname)
{
this.sname=sname;
}
public String getSname()
{
return sname;
}
};
To access encapsulated use fallowing code:-
class Test
{
public static void main(String[] args)
{
Encapsulation e=new Encapsulation();
e.setSid(100);
e.setSname("hi");
int num=e.getSid();
String name=e.getSname();
System.out.println(num);
System.out.println(name);
}
};

intput and output

output:100,,hi