Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Data Hiding in Java

Last Updated on March 15, 2023 by Prepbytes

Data hiding is one of the important concepts of Object Oriented Programming, which is used to restrict access to certain data members or methods of a class so that they can only be accessed and modified by the class itself and not by external code.

What is Data Hiding in Java?

In Object Oriented Programming, the term data hiding refers to hiding internal object details such as data members from outside users. In simple words, data hiding is used to hide or secure the data of a particular from the outside access of the class.

Important Access Modifiers Associated with the Data Hiding in Java

In Java, there are mainly four access modifiers available, which are listed below.

  • Private: The private keyword is used to declare a class member as private, which means that it can only be accessed within the same class. Private members are not accessible from outside the class, including subclasses.
  • Default: if no access modifier is specified for a class member (field, method, or nested class), it is considered to have default access. This means that the member can be accessed by other classes within the same package but cannot be accessed from outside the package.
  • Protected: the protected access modifier is used to declare a class member (field or method) that can be accessed within the same package or by subclasses in a different package. The protected modifier provides a level of access that is less restrictive than private but more restrictive than public.
  • Public: Public is the most common access modifier in java. The public access modifier is used to declare a class member (field, method, or nested class) that can be accessed from anywhere, including outside the package and by subclasses.

Example of Data Hiding in Java

Let’s take an example to understand data hiding in java:

// program for data hiding in java

public class Employee {
    private int  id;
    private String name;
    private String department;

    public Employee(int id, String name, String department) {
        this.id = id;
        this.name = name;
        this.department = department;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
    
    public static void main(String args[]){
        Employee emp=new Employee(101,"Naimish","Computer");
        System.out.println("Employee Id: "+emp.id);
        System.out.println("Employee Name:"+emp.name);
        System.out.println("Employee Department: "+emp.department);
        
        // Update employee department
        emp.setDepartment("HR");
        System.out.println("\nEmployee details after update:");
        System.out.println("Employee Id: "+emp.id);
        System.out.println("Employee Name:"+emp.name);
        System.out.println("Employee Department: "+emp.department);
    }
}

Output:

Employee Id: 101
Employee Name: Naimish
Employee Department: Computer

Employee details after update:
Employee Id: 101
Employee Name: Naimish
Employee Department: HR

Explanation of the above example:
In this example, the Employee class has three private fields id, name, and department, which are not directly accessible from external code. Instead, the class provides public getter methods getId(), getName(), and getDepartment() to retrieve the values of these fields.

The class also provides a public setter method setDepartment() to allow external code to modify the department field in a controlled way. This method ensures that the department value is valid and consistent with the business logic of the Employee class.

By using private fields and providing public getter and setter methods, the Employee class hides its internal state from external code and allows controlled access to its data. This improves the encapsulation, modularity, and maintainability of the code.

What is the Major Difference between Encapsulation and Data Hiding in Java?

Data hiding refers to the practice of restricting access to certain data members or methods of a class so that they can only be accessed and modified by the class itself and not by external code. On the other side, Encapsulation is a broader concept that refers to the practice of bundling data and methods that operate on that data into a single unit, called a class.

Data hiding is achieved through the use of access modifiers such as private, protected, and public. While, Encapsulation is achieved by defining the data members of a class as private and providing public methods, known as getters and setters, to access and modify the data.

Conclusion
In conclusion, this article will help you to understand what is data hiding in java and what is the difference between encapsulation and data hiding in java. In addition, you will also learn about access modifiers in java and an example to understand data hiding in java better.

FAQs

1. How to achieve data hiding in Java?
In Java, data hiding is achieved by declaring the class fields as private and providing public getter and setter methods to access and modify the field values. This ensures that the internal state of the object is protected from external access or modification while providing controlled and safe access to its data.

2. What are the benefits of data hiding in Java?
Data hiding improves encapsulation and reduces coupling between classes, as it allows the internal implementation details of a class to be changed without affecting the external interface. It also enhances code modularity, as it allows classes to be independently developed and tested. Furthermore, it improves maintainability, as it reduces the risk of data corruption or unintended modifications by external code, and helps to enforce business logic and data integrity.

3. What is the difference between data hiding and abstraction in Java?
Data hiding is a technique used to hide the internal state of a class from the external world, while abstraction is a broader concept that refers to the practice of reducing complexity by modeling systems at a high level of generality. Abstraction is achieved in Java through the use of interfaces, abstract classes, and inheritance, while data hiding is achieved through private fields and accessors.

4. How does data hiding impact the performance of Java programs?
Data hiding in Java can have a minor impact on performance, as it requires the use of accessors and mutators to access private fields. However, the performance impact is usually negligible, especially compared to the benefits of improved modularity, encapsulation, and maintainability.

5. Can data hiding be used in Java interfaces?
Yes, data hiding can be used in Java interfaces by defining constants as private or protected fields and providing public accessors to read their values. However, Java interfaces do not allow the definition of instance variables or non-abstract methods, as they are intended to define contracts and not implementations.

Leave a Reply

Your email address will not be published. Required fields are marked *