Abstraction is a concept where you hide the complex details and only expose the behavior that matters. It's like driving a car — you don't need to know the combustion details happening inside the engine to use the accelerator and brake. In Java, you can build abstraction using abstract classes and interfaces.
java
abstract class Payment {
abstract void pay(double amount);
void showCurrency() {
System.out.println("Currency: USD");
}
}
class CardPayment extends Payment {
void pay(double amount) {
System.out.println("Paid " + amount + " using card.");
}
}
public class Main {
public static void main(String[] args) {
Payment payment = new CardPayment();
payment.showCurrency();
payment.pay(49.99);
}
}Payment is an abstract class, and the pay() method is declared without a body. CardPayment implements the pay method. You can't directly do new Payment() on an abstract class.
You should see
Currency: USD Paid 49.99 using card.Real-World Use
You can design things like payment methods, file storage providers, AI model providers, and notification providers using abstraction.