(Translated by https://www.hiragana.jp/)
Difference Between Data Hiding and Abstraction in Java - GeeksforGeeks
Open In App

Difference Between Data Hiding and Abstraction in Java

Last Updated : 09 Jun, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Abstraction Is hiding the internal implementation and just highlight the set of services. It is achieved by using the abstract class and interfaces and further implementing the same. Only necessarily characteristics of an object that differentiates it from all other objects. Only the important details are emphasized and the rest all are suppressed from the user or reader.

A real-life example of abstraction

By using ATM GUI screen bank people are highlighting the set of services what the bank is offering without highlighting internal implementation. 

Types of Abstraction: There are basically three types of abstraction

  1. Procedural Abstraction
  2. Data Abstraction
  3. Control Abstraction

1. Procedural Abstraction: From the word itself, there are a series of procedures in form of functions followed by one after another in sequence to attain abstraction through classes.

2. Data Abstraction: From the word itself, abstraction is achieved from a set of data that is describing an object.

3. Control Abstraction: Abstraction is achieved in writing the program in such a way where object details are enclosed.

Advantages of Abstraction:

  • Users or communities can achieve security as there are no highlights to internal implementation.
  • The enhancement will become very easy because without affecting end users one is able to perform any type of changes in the internal system
  • It provides more flexibility to end-user to use the system very easily
  • It improves the richness of application

Implementation of Abstraction: It is implemented as a class which only represents the important traits without including background detailing. Providing only the necessary details and hiding all its internal implementation. Below is the java implementation of abstraction: 

Java




// Java program showing the working of abstraction
 
// Importing generic libraries
import java.io.*;
 
// Creating an abstract class
// demonstrate abstraction
abstract class Creature {
 
    // Just providing that creatures has legs
    // Hiding the number of legs
    abstract void No_Of_legs();
}
 
// A new child class is extending
// the parent abstract class above
class Elephant extends Creature {
 
    // Implementation of the abstract method
    void No_Of_legs()
    {
 
        // Printing message of function in non abstract
        // child class
        System.out.println("It has four legs");
    }
}
 
// Again a new child class is extended from parent
//  Human class to override function created above
class Human extends Creature {
 
    // Same function over-riden
    public void No_Of_legs()
    {
 
        // Message printed if this function is called or
        // Implementation of the abstract method
        System.out.println("It has two legs");
    }
}
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating human object showing the implementation
        Human ob = new Human();
 
        ob.No_Of_legs();
 
        // Creating object of above class in  main
        Elephant ob1 = new Elephant();
 
        // Calling the function in main by
        // creating object of above non abstract class
        ob1.No_Of_legs();
        // Implementation of abstraction
    }
}


 
 

Output

It has two legs
It has four legs

Now, jumping onto the second concept though both the concepts are used to achieve encapsulation somehow there is a sleek difference as shown below:

Data Hiding is hiding internal data from outside users. The internal data should not go directly that is outside person/classes is not able to access internal data directly. It is achieved by using an access specifier- a private modifier. 

Note: The recommended modifier for data members is private. The main advantage of data hiding is security

 

Sample for data hiding:

 

class Account {

private double account_balance;

……..

…….

}

Here account balance of each say employee is private to each other being working in the same organization. No body knows account balance of anybody. In java it is achieved by using a keyword ‘private’ keyword and the process is called data hiding.

It is used as security such that no internal data will be accessed without authentication. An unauthorized end user will not get access to internal data. Programmatically we can implement data hiding by declaring data elements as private. Now to access this data or for modification, we have a special method known as getter setter respectively.

Now to access this data or for modification, we have a special method known as getter setter respectively.

Concept involved in data Hiding: Getter and setter

Getter is used to accessing the private data and setter is used to modify the private data only after authentication. In simple terms, it is hiding internal data from outside users. It is used as security such that no internal data will be accessed without authentication. An unauthorized end user will not get access to internal data. Programmatically we can implement data hiding by declaring data elements as private. Now to access this data or for modification, we have a special method known as getter setter respectively.

Now to access this data or for modification, we have a special method known as getter setter respectively. Getter is used to accessing the private data and setter is used to modify the private data only after authentication.

Implementation of Data Hiding:

Java




// Java Program showing working of data hiding
 
// Importing generic libraries
import java.io.*;
 
// Class created named Bank
class Bank {
 
    // Private data (data hiding)
    private long CurBalance = 0;
 
    // Bank_id is checked for authentication
    long bank_id;
    String name;
 
    // Getter function to modify private data
    public long get_balance(long Id)
    {
 
        // Checking whether the user is
        // authorised or unauthorised
 
        // Comparing bank_id of user and the given Id
        // then only it will get access
        if (this.bank_id == Id) {
 
            // Return current balance
            return CurBalance;
        }
 
        // Unauthorised user
        return -1;
    }
    // Setter function
    public void set_balance(long balance, long Id)
    {
        // Comparing bank_id of user and the given Id
        // then only it will get access
        if (this.bank_id == Id) {
            // Update balance in current ID
            CurBalance = CurBalance + balance;
        }
    }
}
 
// Another class created- Employee
public class Emp {
    public static void main(String[] args)
    {
        // Creating employee object of bank type
        Bank _emp = new Bank();
 
        // Assigning employee object values
        _emp.bank_id = 12345;
        _emp.name = "Roshan";
 
        // _emp.get_balance(123456)
        _emp.set_balance(10000, 12345);
        // This will no get access as bank_id is given wrong
        // so
        // unauthorised user is not getting access that is
        // data hiding
 
        long emp_balance = _emp.get_balance(12345);
        // As this time it is valid user it will get access
 
        // Display commands
        System.out.println("User Name"
                           + "  " + _emp.name);
        System.out.println("Bank_ID"
                           + "  " + _emp.bank_id);
        System.out.println("Current Balance"
                           + "  " + emp_balance);
    }
}


 
 

Output:

 

User Name Roshan
Bank_ID 12345
Current Balance 10000

 



Previous Article
Next Article

Similar Reads

Difference between Abstraction and Encapsulation in Java with Examples
Abstraction and Encapsulation are two of the fundamental concepts in Object-Oriented-Programming. They provide features such as code-reusability, overriding, security purpose, data hiding, and implementation hiding, which help to make the program easy to understand, maintain and secure. However, it can be quite confusing for a beginner to understan
6 min read
Difference between Abstraction and Encapsulation in C++
Abstraction: In OOPs, Abstraction is the method of getting information where the information needed will be taken in such a simplest way that solely the required components are extracted, and also the ones that are considered less significant are unnoticed. The concept of abstraction only shows necessary information to the users. It reduces the com
3 min read
Method Hiding in Java
Prerequisite: Overriding in Java If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass. This mechanism happens because the static method is resolved at the compile time. Static method bind during the compile time using the type of reference
3 min read
Instance Variable Hiding in Java
One should have a strong understanding of this keyword in inheritance in Java to be familiar with the concept. Instance variable hiding refers to a state when instance variables of the same name are present in superclass and subclass. Now if we try to access using subclass object then instance variable of subclass hides instance variable of supercl
2 min read
Abstraction by Parameterization and Specification in Java
Abstraction by Parameterization and specification both are important methods in Java. We do this in hope of simplifying our analysis by separating attributes and details implementation for user requirements by showing the essential part to the user and hiding certain details for various purposes like security, maintenance, etc. Abstractions help us
4 min read
Control Abstraction in Java with Examples
Our aim is to understand and implement Control Abstraction in Java. Before jumping right into control abstraction, let us understand what is abstraction. Abstraction: To put it in simple terms, abstraction is nothing but displaying only the essential features of a system to a user without getting into its details. For example, a car and its functio
3 min read
Abstraction in Java
Abstraction in Java is the process in which we only show essential details/functionality to the user. The non-essential implementation details are not displayed to the user. In this article, we will learn about abstraction and what abstraction means. Simple Example to understand Abstraction:Television remote control is an excellent example of abstr
11 min read
Understanding OOPs and Abstraction using Real World Scenario
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. In this article, we will discuss how this OOP's concept is implemented in real world
4 min read
Understanding Encapsulation, Inheritance, Polymorphism, Abstraction in OOPs
As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of t
6 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Difference between Data Warehouse and Data Mart
Both Data Warehouse and Data Mart are used for store the data. The main difference between Data warehouse and Data mart is that, Data Warehouse is the type of database which is data-oriented in nature. while, Data Mart is the type of database which is the project-oriented in nature. The other difference between these two the Data warehouse and the
2 min read
Difference Between Data Science and Data Engineering
Data Science: The detailed study of the flow of information from the data present in an organization's repository is called Data Science. Data Science is about obtaining meaningful insights from raw and unstructured data by applying analytical, programming, and business skills. Data Science is an interdisciplinary field that involves using statisti
6 min read
Difference Between Data Science and Data Mining
Data Science: Data Science is a field or domain which includes and involves working with a huge amount of data and uses it for building predictive, prescriptive and prescriptive analytical models. It's about digging, capturing, (building the model) analyzing(validating the model) and utilizing the data(deploying the best model). It is an intersecti
6 min read
Difference Between Big Data and Data Mining
Big Data: It is huge, large or voluminous data, information or the relevant statistics acquired by the large organizations and ventures. Many software and data storage created and prepared as it is difficult to compute the big data manually. It is used to discover patterns and trends and make decisions related to human behavior and interaction tech
3 min read
Difference Between Data Mining and Data Visualization
Data mining: Data mining is the method of analyzing expansive sums of data in an exertion to discover relationships, designs, and insights. These designs, concurring to Witten and Eibemust be "meaningful in that they lead to a few advantages, more often than not a financial advantage." Data in data mining is additionally ordinarily quantitative par
2 min read
Difference Between Small Data and Big Data
Small Data: It can be defined as small datasets that are capable of impacting decisions in the present. Anything that is currently ongoing and whose data can be accumulated in an Excel file. Small Data is also helpful in making decisions, but does not aim to impact the business to a great extent, rather for a short span of Small data can be describ
3 min read
Difference Between Big Data and Data Warehouse
Big Data: Big Data basically refers to the data which is in large volume and has complex data sets. This large amount of data can be structured, semi-structured, or non-structured and cannot be processed by traditional data processing software and databases. Various operations like analysis, manipulation, changes, etc are performed on data and then
3 min read
Difference Between Data Visualization and Data Analytics
Data Visualization: Data visualization is the graphical representation of information and data in a pictorial or graphical format(Example: charts, graphs, and maps). Data visualization tools provide an accessible way to see and understand trends, patterns in data and outliers. Data visualization tools and technologies are essential to analyze massi
3 min read
Difference Between Data Science and Data Visualization
Data Science: Data science is study of data. It involves developing methods of recording, storing, and analyzing data to extract useful information. The goal of data science is to gain knowledge from any type of data both structured and unstructured. Data science is a term for set of fields that are focused on mining big data sets and discovering t
2 min read
Difference between Big Data and Data Analytics
1. Big Data: Big data refers to the large volume of data and also the data is increasing with a rapid speed with respect to time. It includes structured and unstructured and semi-structured data which is so large and complex and it cant not be managed by any traditional data management tool. Specialized big data management tools are required to sto
4 min read
Difference between Data Cleaning and Data Processing
Data Processing: It is defined as Collection, manipulation, and processing of collected data for the required use. It is a task of converting data from a given form to a much more usable and desired form i.e. making it more meaningful and informative. Using Machine Learning algorithms, mathematical modelling and statistical knowledge, this entire p
2 min read
Difference between Data Analytics and Data Analysis
1. Data Analytics : Analytics is a technique of converting raw facts and figures into some particular actions by analyzing those raw data evaluations and perceptions in the context of organizational problem-solving and also with the decision making. Analytics is the discovery and conversation of significant patterns in data. Especially, precious in
2 min read
Difference between Data Profiling and Data Mining
1. Data Mining :Data mining can be defined as the process of identifying the patterns in a prebuilt database. It extracts aberrant patterns, interconnection between the huge datasets to get the correct outcomes.Data mining, sometimes known as “Knowledge discovery in databases”. We can say that it is a combination of three scientific disciplines i.e
5 min read
Difference Between Data Mining and Data Analysis
1. Data Analysis : Data Analysis involves extraction, cleaning, transformation, modeling and visualization of data with an objective to extract important and helpful information which can be additional helpful in deriving conclusions and make choices. The main purpose of data analysis is to search out some important information in raw data so the d
2 min read
Difference between data type and data structure
Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the compiler where the programmer informs the compile
4 min read
Difference between a Data Analyst and a Data Scientist
Nowadays as we know the roles of Data analyst and Data scientist are often used in extracting insights from the data. Both professionals work with data to get various insights, but their responsibilities, skill sets, and the depth of their involvement in the data analytics process differ significantly. In this article, we will explore the What is D
5 min read
Difference Between Big Data and Data Science
The terms "Big Data" and "Data Science" often emerge as pivotal concepts driving innovation and decision-making. Despite their frequent interchangeability in casual conversation, Big Data and Data Science represent distinct but interrelated fields. Understanding their differences, applications, and how they complement each other is crucial for busi
4 min read
Difference between Data Lake and Data Warehouse
Data LakeData Lake is the concept where all sorts of data can be landed at a low cost but exceedingly adaptable storage/zone to be examined afterward for potential insights. It is another advancement of what ETL/DWH pros called the Landing Zone of data. Only presently we are looking at ALL sorts of information independent of construction, structure
3 min read
Difference between Data Management and Data Governance
Data Management and Data Governance are both critical aspects of handling data within organizations, but they focus on different facets of the data lifecycle and have distinct roles. Data Management involves the comprehensive administration of data throughout its lifecycle, encompassing acquisition, storage, processing, and disposal. It emphasizes
5 min read
Difference between Data Security and Data Integrity
Data protection and integrity have become very important in this modern world for organizations, companies, and even for the people. Unique, basic, and important to the theme here are two concepts Data Security and Data Integrity. Although both oversee separate ideas in the sector of data management they are commonly referred to. Out of these two,
6 min read
three90RightbarBannerImg