Difference Between Data Hiding and Abstraction in Java
Last Updated :
09 Jun, 2023
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
- Procedural Abstraction
- Data Abstraction
- 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
import java.io.*;
abstract class Creature {
abstract void No_Of_legs();
}
class Elephant extends Creature {
void No_Of_legs()
{
System.out.println( "It has four legs" );
}
}
class Human extends Creature {
public void No_Of_legs()
{
System.out.println( "It has two legs" );
}
}
public class GFG {
public static void main(String[] args)
{
Human ob = new Human();
ob.No_Of_legs();
Elephant ob1 = new Elephant();
ob1.No_Of_legs();
}
}
|
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
import java.io.*;
class Bank {
private long CurBalance = 0 ;
long bank_id;
String name;
public long get_balance( long Id)
{
if ( this .bank_id == Id) {
return CurBalance;
}
return - 1 ;
}
public void set_balance( long balance, long Id)
{
if ( this .bank_id == Id) {
CurBalance = CurBalance + balance;
}
}
}
public class Emp {
public static void main(String[] args)
{
Bank _emp = new Bank();
_emp.bank_id = 12345 ;
_emp.name = "Roshan" ;
_emp.set_balance( 10000 , 12345 );
long emp_balance = _emp.get_balance( 12345 );
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