Encapsulation is a fundamental principle of object-oriented programming that hides data and methods within a class. It's also known as data hiding. Programs are encapsulated by declaring variables as private and access to variables is provided through public methods. Suppose program is not encapsulated, variables can be misused. Incase program is encapsulated variables will be under developers control that is developer will decide which data to allow or not to allow. By using encapsulation as a foundation bases we can achieve “java bean class”. Java bean class says declare each variable as private and provide access through getters and setters method. Incase of java bean class each variable should have separate getters and setters method.
Benifits of Encapsulation
1.Data Hiding :
Encapsulation allows you to hide the internal state of an object from the outside world. This prevents unauthorized access and modification of data, ensuring that the object's integrity is maintained.
2.Improved Security:
By restricting access to the internal data of a class, encapsulation enhances the security of the application. Only authorized methods can modify the data, reducing the risk of misuse.
3.Modularity:
Encapsulation promotes modularity by allowing you to group related data and methods into a single unit or class. This makes the code easier to manage and understand.
4.Ease of maintenance:
With encapsulation, changes to the internal implementation of a class do not affect other parts of the program. This makes it easier to update and maintain the code over time.
5.Flexibility and Reusability:
Encapsulation allows you to change the internal implementation of a class without affecting its external interface. This flexibility makes it easier to reuse code in different contexts.
6.Control over Data:
Encapsulation gives developers control over how data is accessed and modified. By using getter and setter methods, developers can enforce rules and constraints on data access and modification.
Example for Encapsulation in java :
class Encapsulation
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
if (age>0)
{
this.age = age;
}else{
System.out.println("Please enter a valid age.");
}
}
}
class Main()
{
public static void main(String[] args)
{
Encapsulation obj = new Encapsulation();
obj.setName("sree");
obj.setAge(27);
System.out.println("Name:" + obj.getName());
System.out.println("Age:" + obj.getAge());
}
}
In the example, the `Encapsulation' class has private variables `name` and `age`. Access to these variables is controlled through public getter and setter methods, which allow for data validation and encapsulation. This ensures that the internal state of the object is protected from unauthorized access and modification.
Encapsulation is a key concept in object-oriented programming that protects data and methods within a class by using private variables and public access methods. This technique enhances data security, modularity, maintainability, and flexibility, allowing developers to control data access and modification. Encapsulation ensures the integrity of a program's internal state through the use of Java bean classes, which implement private variables with getters and setters. The provided Java example demonstrates encapsulation by employing data validation and controlled data access through getter and setter methods.