Abstraction is the process of **hiding implementation details** and showing only **essential features**. It is one of the fundamental concepts of OOP.
Abstraction in Java
What is Abstraction?
Abstraction is the process of **hiding implementation details** and showing only **essential features**. It is one of the fundamental concepts of OOP.
Why Use Abstraction?
- Reduces complexity by hiding implementation details.
- Improves code maintainability and scalability.
- Provides a blueprint for future extensions.
Ways to Achieve Abstraction in Java
- Abstract Classes: Can have both **abstract methods** (without implementation) and concrete methods.
- Interfaces: Define a contract that must be implemented by classes.
1. Abstract Class Example
An abstract class **cannot be instantiated** and may contain both **abstract and concrete methods**.
abstract class Vehicle {
abstract void start(); // Abstract method (no implementation)
void stop() { // Concrete method
System.out.println("Vehicle is stopping...");
}
}
// Car extends Vehicle and provides implementation for start()
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car is starting with a key...");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.start(); // Calls Car's implementation
myCar.stop(); // Calls Vehicle's implementation
}
}
2. Interface Example
An interface **only contains abstract methods** (until Java 8). A class must **implement all its methods**.
interface Animal {
void makeSound(); // Abstract method
}
// Dog implements Animal interface
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Dog barks!");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Calls Dog's implementation
}
}
Difference Between Abstract Class and Interface
Feature | Abstract Class | Interface |
---|---|---|
Method Type | Can have both abstract & concrete methods | Only abstract methods (before Java 8) |
Usage | Used for **common functionality** | Used for **defining a contract** |
Access Modifiers | Can have public, private, protected | Only public methods |
Real-World Example: Payment System
Using abstraction, we can define a **payment system** that supports multiple payment methods.
interface Payment {
void processPayment(double amount);
}
// CreditCard implements Payment
class CreditCard implements Payment {
@Override
public void processPayment(double amount) {
System.out.println("Processing Credit Card payment of $" + amount);
}
}
// PayPal implements Payment
class PayPal implements Payment {
@Override
public void processPayment(double amount) {
System.out.println("Processing PayPal payment of $" + amount);
}
}
public class PaymentExample {
public static void main(String[] args) {
Payment payment1 = new CreditCard();
payment1.processPayment(100.50);
Payment payment2 = new PayPal();
payment2.processPayment(75.00);
}
}
Conclusion
Abstraction in Java helps in creating **scalable, maintainable, and flexible** applications.
📌 Next Topic: Encapsulation in Java