Encapsulation

Encapsulation is the process of wrapping up of data (properties) and behaviour (methods) of an object into a single unit, and the unit here is a Class (or interface). English meaning of Encapsulate is to enclose or be enclosed in or as if in a capsule.

In Java, everything is enclosed within a class or interface, unlike languages such as C and C++, where we can have global variables outside classes. Encapsulation enables data hiding, hiding irrelevant information from the users of a class and exposing only the relevant details required by the user. We can expose our operations hiding the details of what is needed to operate. We can protect the internal state of an object by hiding its attributes from the outside world (by making it private), and then exposing them through setter and getter methods. Now the modifications to the object internals are only controlled through these methods. To relate this to the real world, consider the automatic transmission on an automobile.

Consider the example of a linked list’s getsize method. We might be now using a variable named size that is updated on every insert/delete operation. Later we might decide to traverse the list and find size every time someone asks for size. But if some code were directly accessing the size variable, we would have to change all those code for this change. However if we were accessing the size variable through a getsize method, other code can still call that method, and we can do our changes in that method.

Setters and Getters

A setter is a method used to change the value of an attribute and a getter is a method used to get the value of an attribute. There is a standard naming convention for getters and setters, but Java compiler won’t complain even otherwise.

		private String name; 
		public String getName() { 
		return name; 
		} 
		public void setName(String name) { 
		this.name=name; 
		}
	

Here getName and setName are the getter and setter for the variable ‘name’ respectively. Since this is a standard naming convention, many IDEs like eclipse will generate it for you in this form.

Simple Example of Encapsulation
		class EncapsulationCompany{
		    private int Id;
		    private String empName;
		    private int empSalary;

		    //Getter and Setter methods
		    public int getEmpId(){
		        return Id;
		    }

		    public String getEmpName(){
		        return empName;
		    }

		    public int getEmpSalary(){
		        return empSalary;
		    }

		    public void setEmpSalary(int newValue){
		        empSalary = newValue;
		    }

		    public void setEmpName(String newValue){
		        empName = newValue;
		    }
		  
		    public void setEmpId(int newValue){
		        Id = newValue;
		    }
		}
		 class EncapsTest{
		    public static void main(String args[]){
		         EncapsulationCompany obj = new EncapsulationCompany();
		         obj.setEmpName("Ajay");
		         obj.setEmpSalary(24000);
		         obj.setEmpId(1223);
		         System.out.println("Employee Name: " + obj.getEmpName());
		         System.out.println("Employee ID: " + obj.getEmpId());
		         System.out.println("Employee Salary: " + obj.getEmpSalary());
		    } 
		}
	
Output

Employee Name: Ajay

Employee ID: 1223

Employee Salary: 24000

In the above example, all the three data members (or data fields) are private(see: Access Modifiers in Java) which cannot be accessed directly. These fields can be accessed via public methods only. Fields empName, Id and empSalary are made hidden data fields using the encapsulation technique of OOPs.

Advantages of Encapsulation
  • The main advantage of Encapsulation is; it secures our data.
  • It provides us to control over the data.
  • It is a way to achieve data hiding in Java because other class will not access the data through the private data members.
  • The fields of a class can be made read-only or write-only.
  • In encapsulation, class is easy to test.

Copyright 1999- Ducat Creative, All rights reserved.