Data Hiding

Data Hiding

In this tutorial, we will discuss data hiding in Java. Data hiding is a software development technique primarily used in object oriented programming (OOP) to hide internal object details (data members).

Data Hiding
  • The data should not go out directly i.e., outside person is not allowed to access the data this is nothing but “Data Hiding”.
  • The main advantage of data hiding is we can achieve security.

In Java, data hiding is primarily achieved through the use of access modifiers, such as private, protected, and public, to control the visibility and accessibility of class members (fields, methods, constructors) from other classes.

These access modifiers control the visibility of classes, methods, and variables within a Java program, allowing developers to encapsulate implementation details and restrict access to certain parts of the code.

Here’s how data hiding is typically implemented in Java:

Private Access Modifier

When a class member (field or method) is declared as private, it can only be accessed within the same class. This ensures that the member is hidden from external classes and can only be accessed or modified through public methods provided by the class. Private members are commonly used to encapsulate internal state and behavior, preventing direct access and manipulation by external code.

package com.ashok.java;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
   private int id;
   private String name;
   private String email;
   private String sex;
   private String address;
   
   private void myPrivateMethod() {
        // Implementation details hidden from other classes
    }
}
  • It is highly recommended to declare data members with private modifier.
2. Protected Access Modifier

When a member is marked as protected, it is accessible within the same package and by subclasses (whether in the same package or in a different package). This allows for limited visibility to subclasses while still hiding the member from unrelated classes outside the package.

package com.ashok.java;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
   protected int id;
   protected String name;
   protected String email;
   protected String sex;
   protected String address;
   
   protected void myProtectedMethod() {
        // Implementation details accessible to subclasses and classes in the same package
    }
}
3. Package-private (Default) Access Modifier

If no access modifier is specified, the member has package-private visibility, meaning it is accessible only within the same package. This allows for sharing data and methods within a package while hiding them from classes outside the package.

package com.ashok.java;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
   int id;
   String name;
   String email;
   String sex;
   String address;
   
   void packagePrivateMethod() {
        // Implementation details accessible to classes in the same package
   }
}
4. Public Access Modifier

When a member is marked as public, it is accessible from any other class, regardless of package. This provides full visibility and access to the member’s implementation details.

package com.ashok.java;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
   public int id;
   public String name;
   public String email;
   public String sex;
   public String address;
   
   public void packagePrivateMethod() {
        // Implementation details accessible to classes in the same package
   }
}

By appropriately using access modifiers, Java developers can effectively hide implementation details, limit access to sensitive data, and ensure encapsulation and modularity in their code, thus enhancing security and maintainability.

That’s all about data hiding in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Data Hiding
Scroll to top