0/30 Days Completed 0 Learning Days
Day 21 of 30 · Week 3 — OOPS Fundamentals
Abstraction and abstract classes
1 Learn
Abstraction means showing what something does and hiding the details of how. You press a TV remote button without knowing the circuits.
An abstract class cannot be instantiated directly. It may contain abstract methods (no body) that subclasses must implement, as well as normal methods.
2 See the Code
Java
abstract class Payment {
abstract void pay(double amount);
void receipt() {
System.out.println("Receipt printed");
}
}
class UpiPayment extends Payment {
void pay(double amount) {
System.out.println("Paid " + amount + " by UPI");
}
}
public class Shop {
public static void main(String[] args) {
Payment p = new UpiPayment();
p.pay(250);
p.receipt();
}
}- pay() is abstract — every concrete subclass must write it.
- receipt() is a normal method shared by all payments.
- new Payment() would be an error because Payment is abstract.
Output
Paid 250.0 by UPI Receipt printed
3 Practice
- Add a CardPayment class that extends Payment and prints Paid ... by Card.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. Can you write new Payment() if Payment is abstract?
2. An abstract method has...
Step 5
Answer and check the Quick Check above to unlock this.