| Feature | Abstraction | Encapsulation | Data Hiding |
|---|---|---|---|
| Definition | The process of hiding the implementation details and showing only the functionality. | The process of wrapping data and methods that operate on the data within a single unit, usually a class. | The practice of restricting access to certain details or information of an object. |
| Purpose | To reduce complexity and allow the user to interact with an object at a high level. | To protect the data and ensure that it is accessed and modified only through well-defined interfaces. | To ensure that internal object details are not accessible from outside the object. |
| Mechanism | Achieved using abstract classes and interfaces. | Achieved using classes with private fields and public methods. | Achieved using access modifiers like private, protected, and public. |
| Visibility | Hides implementation details, shows only relevant functionality. | Wraps data and methods, but does not necessarily hide data. | Hides data by making it inaccessible from outside the class. |
| Example | An interface Shape with a method draw(). |
A class Person with private fields and public getter and setter methods. |
Private fields in a class Account that are accessible only through public methods. |
| Control | Controls what functionality is exposed. | Controls how data and methods are bundled together. | Controls who can access the data within an object. |
| Focus | Focuses on what an object does. | Focuses on how the object’s data and methods are bundled. | Focuses on protecting the data from unauthorized access. |
| Implementation | Using abstract classes and interfaces to define methods without implementations. | Using classes to define and encapsulate data and methods together. | Using access modifiers (private, protected) to restrict direct access to data. |
Abstraction Example:
abstract class Shape {
abstract void draw(); // abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing Rectangle");
}
}
public class TestAbstraction {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.draw();
s2.draw();
}
}
Encapsulation Example:
class Person {
private String name; // private field
private int age; // private field
public String getName() {
return name; // public getter
}
public void setName(String name) {
this.name = name; // public setter
}
public int getAge() {
return age; // public getter
}
public void setAge(int age) {
this.age = age; // public setter
}
}
public class TestEncapsulation {
public static void main(String[] args) {
Person p = new Person();
p.setName("John");
p.setAge(30);
System.out.println("Name: " + p.getName());
System.out.println("Age: " + p.getAge());
}
}
Data Hiding Example:
class Account {
private double balance; // private field
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance; // public getter
}
public void deposit(double amount) {
if(amount > 0) {
balance += amount; // modifying private field through public method
}
}
public void withdraw(double amount) {
if(amount > 0 && balance >= amount) {
balance -= amount; // modifying private field through public method
}
}
}
public class TestDataHiding {
public static void main(String[] args) {
Account acc = new Account(500.00);
acc.deposit(150.00);
System.out.println("Current Balance: " + acc.getBalance());
acc.withdraw(100.00);
System.out.println("Current Balance: " + acc.getBalance());
}
}
Understanding the differences between abstraction, encapsulation, and data hiding is crucial for designing robust and maintainable object-oriented systems. Abstraction focuses on hiding the complex implementation details and exposing only the essential features. Encapsulation binds data and methods together into a single unit, providing a clear interface for interaction. Data hiding ensures the internal state of an object is protected from unauthorized access and modification, enhancing the security and integrity of the application.