Encapsulation is one of the key features of object-oriented programming. Encapsulation refers to the bundling of fields and methods inside a single class.
It prevents outer classes from accessing and changing fields and methods of a class. This also helps to achieve data hiding.
Advantages of Encapsulation
- Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of the class. It will not be visible to the user how the class is storing values in the variables. The user will only know that we are passing the values to a setter method and variables are getting initialized with that value.
- Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirements. If we wish to make the variables read-only then we have to omit the setter methods like setName(), setAge(), etc. from the above program or if we wish to make the variables write-only then we have to omit the get methods like getName(), getAge(), etc. from the above program
- Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.
- Testing code is easy: Encapsulated code is easy to test for unit testing.
- Freedom to programmer in implementing the details of the system: This is one of the major advantage of encapsulation that it gives the programmer freedom in implementing the details of a system. The only constraint on the programmer is to maintain the abstract interface that outsiders see.
Disadvantages of Encapsulation
- Can lead to increased complexity, especially if not used properly.
- Can make it more difficult to understand how the system works.
- May limit the flexibility of the implementation.
Implement Encapsulation
- Declare the class variables as
private
- Provide
public
methods to access and update the values of the private variables
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class Person { // Private member variables private String name; private int age; // Getter method for the name variable public String getName() { return name; } // Setter method for the name variable public void setName(String name) { this.name = name; } // Getter method for the age variable public int getAge() { return age; } // Setter method for the age variable public void setAge(int age) { if(age > 0) { // Adding some validation this.age = age; } } } public class Main { public static void main(String[] args) { Person person = new Person(); // Using setter methods to set values person.setName("John"); person.setAge(25); // Using getter methods to get values System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } } |
Data Hiding
Data hiding is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Person { // private field private int age; // getter method public int getAge() { return age; } // setter method public void setAge(int age) { this.age = age; } } class Main { public static void main(String[] args) { // create an object of Person Person p1 = new Person(); // change age using setter p1.setAge(24); // access age using getter System.out.println("My age is " + p1.getAge()); } } |
Output:
1 | My age is 24 |