Abstraction and data hiding are important concepts in Java used to reduce complexity and protect the internal details of a class. Abstraction focuses on hiding implementation details and exposing only the essential functionality, whereas data hiding focuses on restricting direct access to an object's internal data.
- Abstraction is mainly achieved using abstract classes and interfaces.
- Data hiding is mainly achieved using access modifiers such as private.
Abstraction in Java
Abstraction is the process of hiding implementation details and exposing only the essential features or functionality to the user. In Java, abstraction can be achieved using abstract classes and interfaces.
- Shows only essential functionality.
- Achieved using abstract classes and interfaces.
abstract class Creature {
// Abstract method
abstract void numberOfLegs();
}
class Human extends Creature {
@Override
void numberOfLegs() {
System.out.println("Human has two legs");
}
}
class Elephant extends Creature {
@Override
void numberOfLegs() {
System.out.println("Elephant has four legs");
}
}
public class Main {
public static void main(String[] args) {
Creature human = new Human();
Creature elephant = new Elephant();
human.numberOfLegs();
elephant.numberOfLegs();
}
}
Output
Human has two legs Elephant has four legs
Explanation: Creature defines the abstract behavior numberOfLegs() without providing its implementation. Human and Elephant provide their own implementations. The user only needs to know that each creature can provide its number of legs; the specific implementation is hidden.
Data Hiding in Java
Data hiding is the process of restricting direct access to the internal data of a class. It is commonly achieved by declaring data members as private and providing controlled access through methods such as getters and setters.
- Provides controlled access through methods.
- Helps prevent unwanted modification of data.
class BankAccount {
// Private data member
private double balance;
// Constructor
BankAccount(double balance) {
this.balance = balance;
}
// Getter
public double getBalance() {
return balance;
}
// Controlled method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(10000);
account.deposit(5000);
System.out.println("Balance: " + account.getBalance());
}
}
Output
Balance: 15000.0
Explanation: balance is declared private, so it cannot be accessed directly from outside BankAccount. The getBalance() method provides controlled read access, while deposit() controls how the balance can be modified. This protects the internal state of the object.
Abstraction vs Data Hiding
| Feature | Abstraction | Data Hiding |
|---|---|---|
| Meaning | Hides implementation details | Restricts direct access to data |
| Main Focus | What an object does | Protecting internal data |
| Hides | Implementation details | Data members |
| Main Purpose | Reduce complexity | Protect object state |
| Achieved Using | Abstract classes and interfaces | private and other access modifiers |
| Access | Exposes essential functionality | Provides controlled access to data |
| Example | ATM hides how transactions are processed | Bank account hides its balance |
| Related Concept | Inheritance and polymorphism | Encapsulation |
| Primary Benefit | Simplicity and flexibility | Data protection and controlled modification |