Course Content
Core Java
About Lesson

It is the mechanism by which the code and the data that the code manipulates are bound together. The data (attributes) and methods are within a single unit (class). It keeps the code and data safe from misuse and outside access & manipulation. It hides the internal implementation details of a class from the outside world and provides controlled access to the data through methods. Access modifiers (public, private, protected) are used to control the visibility of class members.

The basis of encapsulation is Class.

// Define a class named InstagramUser
public class InstagramUser {
    // Attributes or fields
    private String username;
    private String fullName;
    private int followersCount;
    private int followingCount;
    private boolean verified;

    // Constructor method
    public InstagramUser(String username, String fullName) {
        this.username = username;
        this.fullName = fullName;
        this.followersCount = 0; // Initially, the user has 0 followers
        this.followingCount = 0; // Initially, the user is not following anyone
        this.verified = false;   // Initially, the user is not verified
    }

    // Method to follow another user
    public void follow(InstagramUser user) {
        user.followersCount++;   // Increment the followers count of the user being followed
        this.followingCount++;   // Increment the following count of the current user
    }

    // Method to display user information
    public void displayInfo() {
        System.out.println("Username: " + username);
        System.out.println("Full Name: " + fullName);
        System.out.println("Followers: " + followersCount);
        System.out.println("Following: " + followingCount);
        System.out.println("Verified: " + (verified ? "Yes" : "No"));
    }

    // Getter and setter methods for the verified attribute
    public boolean isVerified() {
        return verified;
    }

    public void setVerified(boolean verified) {
        this.verified = verified;
    }
}  
 	
   		// Create an InstagramUser object
      InstagramUser user1 = new InstagramUser("john_doe", "John Doe");

      // Accessing attributes using getter methods
      System.out.println("Username: " + user1.getUsername());
      System.out.println("Full Name: " + user1.getFullName());
      System.out.println("Followers: " + user1.getFollowersCount());
      System.out.println("Following: " + user1.getFollowingCount());
      System.out.println("Verified: " + (user1.isVerified() ? "Yes" : "No"));

      // Follow another user
      InstagramUser user2 = new InstagramUser("jane_smith", "Jane Smith");
      user1.follow(user2);

      // Display updated information after following
      System.out.println("Updated Followers count: " + user1.getFollowersCount());
      System.out.println("Updated Following count: " + user1.getFollowingCount());

InstagramUser class encapsulates the attributes such as username, fullName, followersCount, followingCount, and verified using private access modifiers.

Getter methods (getUsername(), getFullName(), getFollowersCount(), getFollowingCount(), isVerified()) provide read-only access to these attributes.

The setVerified() method allows controlled modification of the verified attribute.

Advantages of Encapsulation

  1. 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. The user will only know that we are passing the values to a setter method and variables are getting initialized with that value.
  2. 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
  3. Improved Maintainability: By encapsulating the data, the internal implementation of a class can be changed without affecting other parts of the code that use the class. This makes it easier to update and maintain the codebase. Changes to the internal workings of a class do not require changes to external code.
  4. Enhanced Security: Encapsulation restricts direct access to an object’s fields, providing controlled access through getter and setter methods. This control can enforce validation and security checks, making the application more secure and robust.
  5. Testing code is easy: Encapsulated code is easy to test for unit testing.
  6. Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.

Scroll to Top