47 Java - Encapsulation
Java Encapsulation
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
Achieving Encapsulation in Java
To achieve encapsulation in Java −
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the variables values.
Java Encapsulation Example
Following is an example that demonstrates how to achieve Encapsulation in Java −
/* File name : EncapTest.java */ public class EncapTest { private String name; private String idNum; private int age; public int getAge() { return age; } public String getName() { return name; } public String getIdNum() { return idNum; } public void setAge( int newAge) { age = newAge;} public void setName(String newName) { name = newName; } public void setIdNum( String newId) { idNum = newId; } }
The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any class that wants to access the variables should access them through these getters and setters.
The variables of the EncapTest class can be accessed using the following program −
/* File name : RunEncap.java */ public class RunEncap { public static void main(String args[]) { EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); } } public class EncapTest { private String name; private String idNum;private int age; public int getAge() { return age; } public String getName() { return name; } public String getIdNum() { return idNum; } public void setAge( int newAge) { age = newAge; } public void setName(String newName) { name = newName; } public void setIdNum( String newId) { idNum = newId; } }
Output
Name : James Age : 20
Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
Java Encapsulation: Read-Only Class
A read-only class can have only getter methods to get the values of the attributes, there should not be any setter method.
Example: Creating Read-Only Class
In this example, we defined a class Person with two getter methods getName() and getAge(). These methods can be used to get the values of attributes declared as private in the class.
Output
Name of the person is: Robert Age of the person is: 21
Java Encapsulation: Write-Only Class
A write-only class can have only setter methods to set the values of the attributes, there should not be any getter method.
Example: Creating Write-Only Class
In this example, we defined a class Person with two setter methods setName() and setAge(). These methods can be used to set the values of attributes declared as private in the class.
Java Encapsulation: More Examples
Example 1: Person Class (Fully Encapsulated)
This example creates a fully encapsulated class named "Person". This class has private class attributes, setter, and getter methods.
Output
Person 1: Name : Robert Age : 21 Person 2: Name : Riyan Age : 22
Example 2: Employee Class (Fully Encapsulated)
This example creates a fully encapsulated class named "Employee". This class has private class attributes, setter, and getter methods.
Output
Employee (Intial Values): EMP001 , Robert , 75450.0 Employee (Updated Values): EMP002 , Riyan , 90500.0

Comments
Post a Comment