Data Hiding

Data Hiding in Java

Data hiding is a crucial concept in object-oriented programming (OOP) that involves restricting the direct access to class attributes (data) from outside the class. It aims to encapsulate the data within the class, preventing unauthorized access and ensuring controlled interaction through defined methods. By hiding the internal representation of data, we can protect it from accidental modification or misuse, enhancing code security and maintainability.

To achieve data hiding in Java, follow these practices:

  • Declare Data Members as Private: Make the class attributes private to limit direct access from outside the class.
  • Provide Access via Getter and Setter Methods: Implement public methods (getters and setters) to read and modify the private attributes, ensuring controlled access and maintaining encapsulation.

Here are two examples demonstrating data hiding in Java:

Example 1: Data Hiding with Private Attributes 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());
          }
      }
    
  

Example 2: Data Hiding with Read-Only Access

    
      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 Example 1, the "Person" class demonstrates data hiding by declaring private attributes "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 demonstrates data hiding with read-only access to data. The "Circle" class has a private attribute "radius" and provides a public getter method to retrieve its value. 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