Abstract Class
Abstract Class
A class that cannot be instantiated. Defines a common interface. Implemented through inheritance.
Abstract Class
A class that cannot be instantiated. Defines a common interface. Implemented through inheritance.
An Abstract Class is a class that cannot be instantiated directly and can only be used through inheritance. It contains one or more abstract methods and forces child classes to implement them. In Java it is defined with the abstract keyword, and in Python with the ABC class and the @abstractmethod decorator from the abc module.
The core purpose of an abstract class is to implement the "Template Method pattern." Common logic is implemented in the abstract class, and only the parts that vary are declared as abstract methods for child classes to override. For example, in a payment system, a PaymentProcessor abstract class defines the validate-process-complete flow, and each payment method (card, bank transfer, simple pay) implements only its own actual processing logic.
It is important to understand the difference from an Interface. An Interface is 100% abstract and has no implementation code, whereas an Abstract Class can contain partial implementation. Since Java 8 added default methods to interfaces, the boundary has blurred, but being able to hold state (fields) is still unique to Abstract Classes. In practice, choose an Abstract Class for an "is-a" relationship and an Interface for a "can-do" relationship.
In large projects, abstract classes greatly improve code reusability and maintainability. As with Spring Framework's AbstractController and Android's BaseActivity pattern, they gather common functionality in one place and let the whole team develop with a consistent structure. However, inheritance creates tight coupling, so avoid excessive inheritance hierarchies and prefer composition where possible.
// Java - 결제 시스템 추상 클래스 예제
public abstract class PaymentProcessor {
protected String merchantId;
protected double amount;
public PaymentProcessor(String merchantId, double amount) {
this.merchantId = merchantId;
this.amount = amount;
}
// 템플릿 메서드: 전체 결제 흐름 정의
public final PaymentResult process() {
validate(); // 1. 검증 (공통)
PaymentResult result = executePayment(); // 2. 실제 결제 (추상)
saveTransaction(result); // 3. 저장 (공통)
return result;
}
// 공통 구현
protected void validate() {
if (amount <= 0) {
throw new IllegalArgumentException("결제 금액은 0보다 커야 합니다");
}
System.out.println("검증 완료: " + amount + "원");
}
protected void saveTransaction(PaymentResult result) {
System.out.println("거래 저장: " + result.getTransactionId());
}
// 추상 메서드: 자식 클래스에서 반드시 구현
protected abstract PaymentResult executePayment();
public abstract String getPaymentMethod();
}
// 구체 클래스: 카드 결제
public class CardPayment extends PaymentProcessor {
private String cardNumber;
public CardPayment(String merchantId, double amount, String cardNumber) {
super(merchantId, amount);
this.cardNumber = cardNumber;
}
@Override
protected PaymentResult executePayment() {
// 실제 카드 결제 로직
String txId = "CARD-" + System.currentTimeMillis();
System.out.println("카드 결제 처리: " + cardNumber.substring(0, 4) + "****");
return new PaymentResult(txId, true, amount);
}
@Override
public String getPaymentMethod() {
return "CREDIT_CARD";
}
}
// 사용 예시
PaymentProcessor payment = new CardPayment("SHOP001", 50000, "1234567890123456");
PaymentResult result = payment.process();
// 출력:
// 검증 완료: 50000.0원
// 카드 결제 처리: 1234****
// 거래 저장: CARD-1706123456789
"When designing the payment module, I suggest making PaymentProcessor an abstract class. Validation and logging stay in the parent as common logic, and only the actual payment logic needs to be implemented for each PG provider. Even if we add Toss Payments later, we can plug it in right away by just implementing the abstract methods."
"The criterion for choosing between an Abstract Class and an Interface is 'the nature of the relationship.' An Animal abstract class expresses the Dog 'is-a' Animal relationship, and a Flyable interface expresses Bird's 'can-do' fly ability. Also, an abstract class can hold state, so it can share protected fields with child classes, but in Java it has the downside of the single-inheritance limit."
"Nice that you put final on this process() method. In the Template Method pattern, you need to prevent child classes from overriding the entire flow. And you left validate() open as protected so it can be extended when needed. That balance is just right."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Inheritance three or more levels deep, like AbstractBase → AbstractMiddle → AbstractDetail → ConcreteClass, is a debugging nightmare. Keep the inheritance depth within 2 levels, and refactor to composition if needed.
If every method is abstract, use an Interface. The value of an Abstract Class lies in the combination of 'common implementation + extension points.' Declare as abstract only the parts that truly need to change.
The error appears only at runtime, so bugs are found late. With ABC and @abstractmethod, a TypeError is raised right at instantiation, giving you fast feedback.
In Java, declare the template method that defines the overall flow as final. It prevents child classes from accidentally overriding the whole logic and clearly conveys the intent: "do not change this method."