Encapsulation is a mechanism in object-oriented programming that wraps data and the functions that operate on it into a single unit. It allows access and modification of private data members of the class through getters and setters.
When we create a class, we achieve encapsulation because a class allows us to declare private data members and functions together into a single unit. Encapsulation is also known as data hiding and data binding.
Data Hiding - Declaring the data member of the class as private so other class members can’t access from outside of the class directly known as data hiding.
Why Encapsulation?
There is a class called Student
, and this class has properties such as age, name, and city.
public class Student {
public int age; //Data member
public String name;
public String city;
}
In this example, anyone can access these data members, change their values, and also give the wrong values to the data members.
public class MasterBackend {
public static void main(String[] args) {
Student student = new Student();
student.age = -18; // Assigning a negative age
student.name = "Ayush Shrivastava";
student.city = "India";
System.out.println("Student Details:");
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.age);
System.out.println("City: " + student.city);
}
}
In the above example, Ayush has assigned his age as a negative value, so there is no control over the data. Anyone can access or change the data. That’s why encapsulation is important in Java. So now we understand how we can take control of data members.
Data Binding: Wrapping the Data members as private and creating the getter and setter to view and modify the data member is known as Data binding.
How to Achieve?
There are two steps to achieving Encapsulation.
Make the members of the class private.
Creating the getter and setter methods to view and modify the data member.
Setter
Method to set or modify the value of private data members.
void setDataMemberName(data_type memberName)
{
data_member_name= memberName;
}
Getter
Method to retrieve the value of a private data member.
return_type getDataMemberName()
{
return data_member_name;
}
Example:
Now we understand how to achieve Encapsulation with an example
public class Car {
private String model; //Declaring the data member as private
private String color;
private int speed;
// Getter and Setter for view and mofifying the data mebers of the class
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
// Getter and Setter for color
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// Getter and Setter for speed
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed >= 0) { // Ensuring speed is non-negative
this.speed = speed;
} else {
System.out.println("Speed cannot be negative.");
}
}
}
In the above example, we defined a Car
class and set its data members as private to ensure data hiding. We also implemented getter and setter methods to access and modify the data members.
public class MasterBackend {
public static void main(String[] args) {
Car car = new Car();
car.setModel("Tesla Model S");
car.setColor("Red");
car.setSpeed(-100); // Attempting to set a negative speed
System.out.println("Car Details:");
System.out.println("Model: " + car.getModel());
System.out.println("Color: " + car.getColor());
System.out.println("Speed: " + car.getSpeed());
}
}
In this class, one can’t assign the value of speed as negative because we get more control of data with the setSpeed()
method. That’s why encapsulation is important.
What is a Java Bean?
A Java Bean is a simple Java class that follows specific conventions, such as having a no-argument constructor, private fields, and public getter and setter methods. It is mainly used for encapsulating data and easy reusability in Java applications.
What is shadowing in Java?
In encapsulation, shadowing occurs when a local variable or method parameter hides a class variable (instance variable) with the same name, making it difficult to access the class variable. We use this
to reference the class variable.
public class Car {
private String model; //Declaring the data member as private
private String color;
private int speed;
// Getter and Setter for view and mofifying the data mebers of the class
public String getModel() {
return model;
}
public void setModel(String model /* local variable */) {
this.model = model;
}
// Getter and Setter for color
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// Getter and Setter for speed
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed >= 0) { // Ensuring speed is non-negative
this.speed = speed;
} else {
System.out.println("Speed cannot be negative.");
}
}
}
Here, the name of the instance variable model
and the name of the local variable of the setter method are also the same, so the shadowing happens here. With the help of this
keyword, we solve the problem of shadowing in Java.
Advantages
Encapsulation provides data security because private data cannot be accessed from non-member functions outside of the class. Secondly, encapsulation also provides data validation.