This document discusses encapsulation in Java. Encapsulation involves wrapping up data and behavior into a single unit called a class. It enables data hiding by making attributes private and exposing them through public getter and setter methods. This allows control over modifying object internals and protects data. An example demonstrates a class with private fields for employee ID, name, and salary that are accessed via getter and setter methods. The advantages of encapsulation are that it secures data, provides data control and hiding, and makes classes easier to test.
1. Welcome to Ducat India
Language | Industrial Training | Digital Marketing | Web
Technology | Testing+ | Database | Networking | Mobile
Application | ERP | Graphic | Big Data | Cloud Computing
Apply Now
Call us:
70-70-90-50-90
www.ducatindia.com
2. Encapsulation is the process of wrapping up of data (properties) and behavior (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 get size 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 get size method,
other code can still call that method, and we can do our changes in that method.
3. 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.
4. 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;
}
5. 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());
}
}
6. 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.