Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is Data Hiding in Java?
2.
Implementation of Data Hiding in Java
2.1.
Java
3.
What is Abstraction in Java?
3.1.
Types of Abstraction
3.2.
Implementation of Abstraction in Java
3.3.
Java
4.
Difference Between Data Hiding and Abstraction
5.
Advantages of Abstraction
6.
Frequently Asked Questions
6.1.
How can we hide the data in Java?
6.2.
Why encapsulation is called data hiding?
6.3.
What is encapsulation and data hiding in Java?
6.4.
Why do we hide data in Java?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Abstraction and Data Hiding in Java

Author Aryan Raj
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

Data Hiding vs Abstraction

Also see, Duck Number in Java

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


Output:

User Name  Aryan
Bank_ID  12345
Current Balance  0


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

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


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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

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.

Know What is Object in OOPs here in detail.

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:-

We hope that this blog has helped you enhance your knowledge regarding throw vs throws in Software Engineering and if you would like to learn more, check out our articles on java oops. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
Difference between Abstraction and Encapsulation
Next article
Interfaces
Live masterclass