3. Abstraction in Java
Hiding internal implementation details and exposing only essential functionality to the user.
What is Abstraction?
English Definition
Abstraction means hiding implementation details and showing only essential functionality to the user. Real-life example: an ATM—you click Withdraw or Check Balance without needing to know how bank servers verify your account internally.
Hinglish Explanation
Abstraction ka matlab hai andar ki complex details ko chhupana aur user ko sirf zaroori functionality dikhana (jaise ATM machine). Abstract class mein
abstract methods (bina body ke) aur normal methods dono ho sakte hain.
Main.java — Abstract Class ATM & SBI Implementation
abstract class ATM {
abstract void withdraw(int amount);
abstract void checkBalance();
void welcome() {
System.out.println("Welcome to ATM");
}
}
class SBI extends ATM {
int balance = 1000;
void withdraw(int amount) {
if (amount <= balance) {
balance = balance - amount;
System.out.println("Please collect cash");
System.out.println("Withdraw Amount: " + amount);
} else {
System.out.println("Insufficient Balance");
}
}
void checkBalance() {
System.out.println("Available Bal: " + balance);
}
}
class Main {
public static void main(String args[]) {
ATM atm = new SBI();
atm.welcome();
atm.checkBalance();
atm.withdraw(200);
atm.checkBalance();
}
}
Console OutputWelcome to ATM
Available Bal: 1000
Please collect cash
Withdraw Amount: 200
Available Bal: 800
Interactive ATM Simulator (ATM atm = new SBI();)
Test how the SBI subclass executes the abstract methods withdraw() and checkBalance():
ATM ready. Initial balance = 1000