1. Introduction
Abstraction is one of the core concepts of Object-Oriented Programming (OOP).
π It means hiding internal implementation details and showing only the essential features to the user.
In simple terms:
βWhat to doβ is exposed, but βhow it is doneβ is hidden.
2. Real-Time Analogy
Think of an ATM Machine:
- You insert a card and withdraw money
-
You donβt know how:
- Bank server validates PIN
- Balance is checked
- Cash is processed
π You only see operations, not internal logic β This is abstraction
3. How Abstraction is Achieved in Java
Abstraction can be achieved using:
- Abstract Class (0β100% abstraction)
- Interface (100% abstraction)
4. Example Using Abstract Class
// Abstract class
abstract class Vehicle {
abstract void start(); // abstract method
void fuelType() {
System.out.println("Petrol/Diesel");
}
}
// Concrete class
class Car extends Vehicle {
void start() {
System.out.println("Car starts with key ignition");
}
}
public class AbstractionExample {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
v.fuelType();
}
}
5. Explanation of Code
-
Vehicleis an abstract class -
start()is an abstract method β no implementation -
Carprovides the implementation ofstart() - User interacts with
Vehiclereference β doesn't know internal logic
Output:
Car starts with key ignition
Petrol/Diesel
6. Example Using Interface
interface Payment {
void pay();
}
class CreditCardPayment implements Payment {
public void pay() {
System.out.println("Paid using Credit Card");
}
}
class UpiPayment implements Payment {
public void pay() {
System.out.println("Paid using UPI");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Payment p = new UpiPayment();
p.pay();
}
}
7. Explanation of Code
-
Paymentinterface defines methodpay() - Different classes implement it differently
- User only calls
pay()β doesn't care about internal implementation
8. Key Points
- Hides implementation details
- Shows only functionality
- Improves security and flexibility
- Helps in loose coupling
9. Advantages of Abstraction
- Reduces complexity
- Improves maintainability
- Enhances reusability
- Allows changing implementation without affecting users
10. Summary
- Abstraction = hiding internal details
- Achieved using abstract classes and interfaces
- Focuses on what to do, not how to do
- Widely used in real-time applications
Java Full Stack Developer Roadmap
To master OOP concepts like abstraction, inheritance, and polymorphism:
π https://www.ashokit.in/java-full-stack-developer-roadmap
Promotional Content
Want to master OOP concepts like Abstraction with real-time examples?
Top comments (0)