Encapsulation

Encapsulation in Java

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP) in Java. It refers to the bundling of data (attributes) and methods within a class, while hiding the internal implementation details from external access. Encapsulation provides a protective barrier, ensuring that the object's state can only be accessed and modified through defined methods, thus enhancing data abstraction and code security.

To achieve encapsulation in Java, you can follow these practices:

  1. Declare Data Members as Private: Make the class attributes private to restrict direct access from outside the class.
  2. Provide Getter and Setter Methods: Implement public methods (getters and setters) to access and modify the private attributes of the class, thus providing controlled access to the internal data.

Here are two examples illustrating encapsulation in Java:

Example 1: Encapsulation with Private Data Members and Public Getter/Setter Methods

    
      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;
          }
      }

      // Usage:
      public class Main {
          public static void main(String[] args) {
              Person person = new Person();
              person.setName("John Doe");
              person.setAge(30);

              System.out.println("Name: " + person.getName());
              System.out.println("Age: " + person.getAge());
          }
      }
    
  

In this example, the class "Person" demonstrates encapsulation by declaring private data members "name" and "age" and providing public getter and setter methods to access and modify them. The "Main" class showcases the usage of these getter and setter methods to interact with the encapsulated data.

Example 2: Encapsulation with Read-Only Access to Data

    
      public class Circle {
          private double radius;

          public Circle(double radius) {
              this.radius = radius;
          }

          public double getRadius() {
              return radius;
          }

          public double getArea() {
              return Math.PI * radius * radius;
          }
      }

      // Usage:
      public class Main {
          public static void main(String[] args) {
              Circle circle = new Circle(5.0);
              System.out.println("Radius: " + circle.getRadius());
              System.out.println("Area: " + circle.getArea());
          }
      }
    
  

In this example, demonstrated encapsulation with read-only access to data. The "Circle" class has a private data member "radius" and provides a public getter method to access it. Additionally, it exposes a public method to calculate the area based on the encapsulated data. The "Main" class illustrates the usage of the read-only access to retrieve the radius and calculate the area of the circle.

Go To OOPS Page Click Here

Post a Comment

Previous Post Next Post