java
  1. java-encapsulation

Java Encapsulation

Encapsulation is one of the core concepts of object-oriented programming (OOP) in Java. It is a mechanism for restricting access to some of the object's components, mainly data members and methods, in order to prevent unauthorized access. Encapsulation is achieved by using access modifiers such as public, private, and protected.

Syntax

Access modifiers are used to enforce encapsulation in Java. The following table shows the different access modifiers and their visibility:

Modifier Class Package Subclass World
public Yes Yes Yes Yes
protected Yes Yes Yes No
default Yes Yes No No
private Yes No No No

To encapsulate a class member, you need to declare it as private, and provide access to it through public getter and setter methods. Here is an example:

public class Person {
   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) {
      this.age = age;
   }
}

In this example, the name and age fields are declared as private, which means they can only be accessed by the Person class. The getName() and getAge() methods are public, which allows external classes to access the values of these fields. The setName() and setAge() methods are also public, which allows external classes to set the values of these fields.

Example

Here is an example of encapsulation using the Person class:

public class Main {
   public static void main(String[] args) {
      Person person = new Person();
      person.setName("John");
      person.setAge(30);
      System.out.println("Name: " + person.getName());
      System.out.println("Age: " + person.getAge());
   }
}

In this example, we create a Person object and set its name and age using the setter methods. We then retrieve these values using the getter methods and print them to the console.

Output

The output of the example above will be:

Name: John
Age: 30

Explanation

Encapsulation is used to protect the data members of an object from unauthorized access. In the example above, we declared the name and age fields as private, which means they can only be accessed by the Person class. We then provided access to these fields through public getter and setter methods.

This ensures that the name and age values can only be accessed and modified through the interface provided by the Person class. This adds an additional layer of protection to the fields and ensures that they are used correctly.

Use

Encapsulation is a fundamental concept in Java and is used to protect the state of an object. It allows developers to control the level of access to the data members of an object and ensures that they are used correctly. Encapsulation is essential in building maintainable and scalable applications.

Important Points

  • Encapsulation is a mechanism for restricting access to an object's data members and methods.
  • In Java, encapsulation is achieved by using access modifiers such as private, public, and protected.
  • Encapsulation ensures that the data members of an object are protected from unauthorized access and modification.
  • Encapsulation is essential in building maintainable and scalable applications.

Summary

Encapsulation is a core concept of OOP in Java, and it is used to protect the data members of an object from unauthorized access. Encapsulation is achieved by declaring the fields as private and providing access to them through public getter and setter methods. Encapsulation is essential in building maintainable and scalable applications.

Published on: