Java

What is Encapsulation ?

What is Encapsulation ?

Encapsulation in java is an important concept, its one of four oops pillar. Encapsulation is process of wrapping code and data together into a single unit.

Encapsulation is also known as Data binding and Data Hiding.

Advantages of Encapsulation:

  • Data Hiding.
  • Code Flexibility.
  • Code Reusability.

Example

public class Employee

{

int employeeid;

String empname;

String address;

int salary;

 

public int getemployeeId () {

return employeeid;

}

 

public void setemployeeId (int empid) {

employeeid = empid;

}

 

public String getname() {

return empname;

}

 

public void setname(String name) {

empname = name;

}

 

public String getaddress() {

return address;

}

 

public void setaddress(String address) {

this.address = address;

}

 

public int getsalary () {

return salary;

}

 

public void setsalary (int sal) {

salary = sal;

}

}

 

public class GetSetEmployee

public static void main(String [] args){

 

Employee obj= new Employee();

 

obj.setemployeeId(10001);

obj.setname(“Ramesh”);

obj.setaddress(“Block C, Street Y, New Delhi”);

obj.setsalary(25000);

 

System.out.println(obj.getemployeeId());

System.out.println(obj.getname());

System.out.println(obj.getaddress());

System.out.println(obj.getsalary());

 

}

}

Output

10001

Ramesh

Block C, Street Y, New Delhi

25000

Leave a Reply

Your email address will not be published. Required fields are marked *