1. What is Abstraction?
Abstraction is one of the core OOP concepts. It is the process of hiding the internal details of how something works and only exposing the essential features or behaviors to the user.
- You don’t show the “how”, only the “what it does”.
- It allows the user to use functionality without worrying about internal implementation.
In Java, abstraction is achieved by:
- Abstract Classes
- Interfaces
2. How Abstraction Works
Key Points:
- Hide Implementation: Users of a class don’t need to know the inner details of methods. They just call the method.
- Only Show Behavior: You provide a method signature (name, input/output), but the internal logic can be hidden.
- Restrict Direct Access: Internal variables can be private so they cannot be accessed directly, only through methods.
Example Using Abstract Class
// Abstract class
abstract class Vehicle {
// Abstract method (no implementation)
abstract void startEngine();
// Regular method (implementation can be provided)
void fuelType() {
System.out.println("Fuel type is Petrol/Diesel/Electric");
}
}
// Child class provides implementation
class Car extends Vehicle {
@Override
void startEngine() {
System.out.println("Car engine starts with a key or button");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car(); // Reference type is abstract class
myCar.startEngine(); // Calls implemented method in Car
myCar.fuelType(); // Calls inherited method
}
}
Explanation:
Vehicle
class hides how the engine starts.Car
class provides the implementation ofstartEngine()
.- The user only interacts with the
startEngine()
method without knowing internal details. - This is abstraction in action.
Example Using Interface
interface RemoteControl {
void turnOn();
void turnOff();
}
class TV implements RemoteControl {
@Override
public void turnOn() {
System.out.println("TV is turned ON");
}
@Override
public void turnOff() {
System.out.println("TV is turned OFF");
}
}
public class Main {
public static void main(String[] args) {
RemoteControl myTV = new TV();
myTV.turnOn(); // Only uses the method
myTV.turnOff(); // No idea how the internal logic works
}
}
Explanation:
RemoteControl
interface only defines what actions are possible.TV
class defines how those actions are executed.- The user is only aware of what methods they can call.
3. Restricting Data and Only Showing Functions
- Use private variables to hide data.
- Provide public methods (getter/setter) to access the data.
- This prevents direct modification and ensures controlled access.
class BankAccount {
private double balance; // Hidden from the user
// Method to deposit money (control access)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
}
}
// Method to check balance
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(500);
System.out.println("Balance: " + account.getBalance());
}
}
- User cannot directly access
balance
. - User can only interact through methods, which is controlled abstraction.
4. Summary of Abstraction
Feature | Description |
---|---|
Purpose | Hide implementation, show only functionality |
How to implement | Abstract classes or interfaces |
Benefits | Simplifies complexity, improves security, allows flexibility |
Restrict data | Use private variables + public methods |
Real-life analogy | Driving a car: you know how to drive it, but not how the engine works |
✅ Key Idea: Abstraction is about hiding the “how” and showing the “what”. It protects your data and makes code easier to use and maintain.