Table of contents
1.
What is Data Hiding in Java?
1.1.
Java
2.
What is Abstraction in Java?
2.1.
Types of Abstraction
2.2.
Java
2.3.
Advantages of Abstraction
3.
Difference Between Data Hiding and Abstraction
4.
Frequently Asked Questions
4.1.
How can we hide the data in Java?
4.2.
Why encapsulation is called data hiding?
4.3.
What is encapsulation and data hiding in Java?
4.4.
Why do we hide data in Java?
5.
Conclusion
Last Updated: Dec 2, 2024
Easy

Difference Between Abstraction and Data Hiding in Java

Author Aryan Raj
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Abstraction and data hiding are key principles of object-oriented programming (OOP) that enhance code security, modularity, and simplicity. Data hiding ensures the encapsulation and security of sensitive data, while abstraction allows the modeling of complex systems. Developers can create flexible, modular, and secure codebases by leveraging these concepts effectively, 
Abstraction and data hiding are some of the main concepts of object-oriented programming(OOPS). Abstraction is a technique for expressing important properties without dealing with the background details. On the other hand, Data Hiding insulates the data by directly accessing it by the application. 

Difference Between Abstraction and Data Hiding in Java

Both concepts appear identical in aspect; however, they are not. The abstraction makes it possible to develop real-world objects with similar properties using user-defined data types. On the other hand, data hiding protects the data and function from unauthorized access.

Abstraction is a technique for expressing important properties without dealing with the background details. On the other hand, Data Hiding insulates the data by directly accessing it by the application.

What is Data Hiding in Java?

Data hiding in Java is a programming technique used to encapsulate information within a class or module, it restricts access to the data from outside the class or module using access modifiers. It is important to enhance code security, reduce unintended interference, and promote modular design by hiding the implementation details of a class.
The term "data hiding" refers to concealing data from program components that don't need to be retrieved. Data hiding helps in the protection of a class's members. Access modifiers such as public, private, and protected are available in programming languages such as Java. Data hiding or information hiding is the process of hiding data from direct access by the program. Encapsulation is used to implement data hiding, which protects a class's data and functions from unauthorized access. On the other hand, Encapsulation occurs when data and functions are encased into a single unit. As a result, data hiding helps in achieving encapsulation. 

The data and functions in a class are made private using data hiding so that they cannot be accessed falsely by functions outside the class and are protected from accidental modification. The functional details of an object can be handled through access specifiers.

data hiding in java


Implementation of Data Hiding in Java

  • Java

Java

// Java Program to demonstrate data hiding
import java.io.*;

// Class Bank
public class Bank {

   // Private data (data hiding)
   private long currBalance = 0;

   // Bank_id is checked for authentication
   long bank_id;
   String name;

   // function to modify private data
   public long getBalance(long Id) {

       // Checking whether the user is
       // valid or not

       // Compare bank_id of user and the give Id
       // then only it will get access
      
       if (this.bank_id == Id) {

       // Return current balance
           return currBalance;
       }

       // Unauthorised user return -1
       return -1;
   }
   // Setter function to update the balance
   public void setBalance(long balance, long Id) {
       // Compare bank_id of user and the give Id
       // then only it will get access
      
       if (this.bank_id == Id) {
       // Update balance in current ID
           currBalance = currBalance + balance;
       }
   }
}

// Another class created- Employee
public class Emp {
   public static void main(String[] args) {
       // Creating employee object of Bank class
       Bank employee = new Bank();

       // Assigning employee object values
       employee.bank_id = 12345;
       employee.name = "Aryan";

       // employee.getBalance(123456)
       employee.setBalance(90000, 1909009);
       // This will not get access as bank_id is given wrong
       // unauthorised user is not getting access. This is data hiding


       long emp_balance = employee.getBalance(12345);
       // As this time it is valid user it will get access


       System.out.println("User Name" + "  " + employee.name);
       System.out.println("Bank_ID" + "  " + employee.bank_id);
       System.out.println("Current Balance" + "  " + emp_balance);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

User Name  Aryan
Bank_ID  12345
Current Balance  0

 

What is Abstraction in Java?

Abstraction hides the internal details and simply displays the user's functionality. It indicates an object's necessary characteristics that distinguish it from other items. To put it in another way, it hides the implementation details while presenting the functionality to the rest of the world. An abstraction concentrates on the external aspect of an object.  It establishes a mental barrier in relation to the viewer's perspective. A proper abstraction emphasizes crucial aspects to the reader or user while suppressing irrelevant and deviant characteristics.

Abstraction may be implemented using abstract classes and interfaces in OOP-supporting programming languages like Java. Abstract and non-abstract methods can be found in an abstract class.

The definitions for the abstract class should be provided by a class that extends an abstract class. Furthermore, an interface's methods are all abstract methods. As a result, a class that implements an interface should have definitions for all of the abstract methods. Data abstraction is implemented as a class representing the essential properties without the accompanying explanations.

Types of Abstraction

  1. Procedural abstraction – It includes a series of instructions with specified functions.
  2. Control abstraction – It is a program control mechanism where interior details are not specified.
  3. Data abstraction – It is a set of data that specifies and describes a data object.
abstraction in java



Implementation of Abstraction in Java

  • Java

Java

package oopsconcept;
public abstract class VehicleAbstract {
   public abstract void start();
   public void stop() {
       System.out.println("Stopping Vehicle in abstract class");
   }
}
class TwoWheeler extends VehicleAbstract {
   @Override
   public void start() {
   System.out.println("Starting Two Wheeler");
   }
}
class FourWheeler extends VehicleAbstract {
   @Override
   public void start() {
   System.out.println("Starting Four Wheeler");
   }
}
package oopsconcept;
public class VehicleTesting {
   public static void main(String[] args) {
       VehicleAbstract my2Wheeler = new TwoWheeler();
       VehicleAbstract my4Wheeler = new FourWheeler();
       my2Wheeler.start();
       my2Wheeler.stop();
       my4Wheeler.start();
       my4Wheeler.stop();
   }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Starting Two Wheeler
Stopping Vehicle in abstract class
Starting Four Wheeler
Stopping Vehicle in abstract class


You can also find the output of this java compiler code here.

Advantages of Abstraction

  • The primary advantage of using an Abstraction in programming is that it allows you to group similar classes together as siblings.
  • Abstraction in Oops(Object-Oriented Programming) helps to reduce the complexity of the design and implementation process.

Difference Between Data Hiding and Abstraction

  • Abstraction is the process of displaying only essential information to the user. But, in Data Hiding, we simply hide data from some components by ensuring access to only limited members.
  • The purpose of having abstraction is to hide the complex implementation details of the software. But data hiding is implemented to have encapsulation in the program.
  • To achieve abstraction, we use abstract classes and interfaces. While for data hiding, we implement getters and setters.
PerametersData HidingAbstraction
DefinitionA concept of concealing the internal implementation details from the user. A concept of showing only the essential features while hiding unnecessary details.
UsageUsed to ensure controlled access and safeguard the data.Used to design a system with minimal complexity while showing only relevant details.
Visibility Details are hidden but can still exist in the program.Irrelevant details are not even defined in the interface presented to the user.
PurposeData hiding protects the data from unauthorized access or modificationAbstraction simplifies the usage and design by focusing on what an object does, not how it does it.
 

Frequently Asked Questions

How can we hide the data in Java?

In Java, data hiding can be achieved through encapsulation. Encapsulation involves declaring data members of a class as private and providing public methods (getters and setters) to manipulate and access these data members. By restricting direct access to the data and controlling it through methods, encapsulation effectively hides the data implementation details from outside classes.

Why encapsulation is called data hiding?

Encapsulation is often referred to as data hiding because it conceals the internal state and implementation details of an object from the outside world. By declaring data members as private and providing controlled access through public methods, encapsulation restricts direct access to the data, thus hiding it and protecting it from unauthorized manipulation.

What is encapsulation and data hiding in Java?

Data hiding focuses on restricting access to an object member within a particular class, while encapsulation concentrates on how the data is accessed and how various objects behave. Data encapsulation is achieved by hiding information and not just concealing the information.

Why do we hide data in Java?

Data hiding is important to make sure that the important data is changed/updated by the members with the correct access levels only. You wouldn't want other classes or objects changing the value of the important parameters of some other class. That could lead to a big issue. Hence not advised.

Conclusion

In this blog, we have discussed the Difference Between Abstraction and Data Hiding in Java. Knowing the difference between abstraction and data hiding in Java helps in writing better and more organized code. Abstraction means focusing on the important parts of a problem and hiding unnecessary details. It helps to create clear and easy-to-understand code by defining clear rules and structures.

Recommended Readings:-

Live masterclass