디지털 상품 구매 시스템
📋 문제 설명
상속과 인터페이스를 동시에 활용하여 온라인 쇼핑몰의 디지털 상품을 모델링하세요.
DigitalProduct 클래스는 Product 클래스를 상속받으면서 동시에 Payable 인터페이스를 구현합니다.
🎯 클래스 구조
Java
Product (부모 클래스)
↓
DigitalProduct (상속 + 인터페이스 구현)
↓
implements Payable (인터페이스)
💻 코드 구조
Product 클래스 (부모 클래스)
Java
class Product {
protected String name;
protected double price;
// 디폴트 생성자
public Product() {
}
// 생성자: 상품 이름과 가격 초기화
public Product(String name, double price) {
// TODO: name과 price를 초기화하세요.
}
// 상품 정보 출력 메서드
public void displayInfo() {
// TODO: 상품명과 가격을 출력하세요.
// 예: "상품명: [name], 가격: [price]원"
}
}
필드
protected String name- 상품명protected double price- 가격
Payable 인터페이스
Java
// 결제 가능한 항목을 위한 인터페이스
interface Payable {
// 상품 가격 반환 메서드
double getPrice();
// 결제 처리 메서드
void processPayment();
}
메서드
getPrice()- 상품 가격 반환processPayment()- 결제 처리
DigitalProduct 클래스 (상속 + 인터페이스 구현)
Java
// Product를 상속받고 Payable을 구현하는 DigitalProduct 클래스
class DigitalProduct extends Product implements Payable {
private String downloadLink;
// 디폴트 생성자
public DigitalProduct() {
}
// 생성자: 상품 이름, 가격, 다운로드 링크 초기화
public DigitalProduct(String name, double price, String downloadLink) {
// TODO: 부모 클래스의 생성자를 호출하고, downloadLink를 초기화하세요.
}
// 상품 정보 출력 메서드 오버라이드
@Override
public void displayInfo() {
// TODO: 부모 클래스의 displayInfo 메서드를 호출하고,
// 추가로 다운로드 링크 정보를 출력하세요.
// 예: "다운로드 링크: [downloadLink]"
}
// Payable 인터페이스의 getPrice 메서드 구현
@Override
public double getPrice() {
// TODO: 상품 가격을 반환하세요.
}
// Payable 인터페이스의 processPayment 메서드 구현
@Override
public void processPayment() {
// TODO: 결제 완료 메시지와 다운로드 링크 전송 메시지를 출력하세요.
// 예: "결제가 완료되었습니다. 다운로드 링크: [downloadLink]"
}
}
추가 필드
private String downloadLink- 다운로드 링크
구현 내용
- 상속:
Product클래스의 기능 물려받음 - 인터페이스 구현:
Payable의 모든 메서드 구현 - 메서드 오버라이드:
displayInfo()를 재정의하여 다운로드 링크 추가
📝 Main 클래스 테스트
Java
public class Main {
public static void main(String[] args) {
// DigitalProduct 객체 생성
DigitalProduct ebook = new DigitalProduct(
"자바 프로그래밍 기초",
29900,
"http://example.com/download/java_ebook"
);
// 상품 정보 출력
System.out.println("=== 상품 정보 ===");
ebook.displayInfo();
System.out.println(); // 빈 줄
// 가격 조회
System.out.println("=== 가격 조회 ===");
System.out.println("가격: " + ebook.getPrice() + "원");
System.out.println(); // 빈 줄
// 결제 처리
System.out.println("=== 결제 처리 ===");
ebook.processPayment();
}
}
예상 출력:
Java
=== 상품 정보 ===
상품명: 자바 프로그래밍 기초, 가격: 29900원
다운로드 링크: http://example.com/download/java_ebook
=== 가격 조회 ===
가격: 29900원
=== 결제 처리 ===
결제가 완료되었습니다. 다운로드 링크: http://example.com/download/java_ebook
💡 학습 포인트
1. 상속과 인터페이스의 동시 사용
- 상속 (
extends): 코드 재사용과 확장 - 인터페이스 구현 (
implements): 특정 기능 보장 - 하나의 클래스가 부모 클래스 상속 + 인터페이스 구현 동시 가능
2. 다중 기능 구현
Java
class DigitalProduct extends Product implements Payable {
// Product의 기능 + Payable의 기능 모두 가짐
}
3. 실무 활용 예시
온라인 쇼핑몰에서 디지털 상품(e-book, 소프트웨어 등)의 특징:
- 일반 상품의 속성 (이름, 가격) → 상속으로 해결
- 즉시 결제 및 다운로드 기능 → 인터페이스로 해결
- 물리적 배송이 아닌 디지털 다운로드
4. 유연한 설계
- 나중에 다른 종류의 디지털 상품(음악, 영화 등) 추가 가능
Payable인터페이스를 구현하면 모든 결제 시스템에서 사용 가능- 상속 계층과 인터페이스 구현을 분리하여 유연성 확보
5. 구문 정리
Java
// 클래스 상속 + 인터페이스 구현
class 자식클래스 extends 부모클래스 implements 인터페이스1, 인터페이스2 {
// 부모 클래스 메서드 사용 가능
// 모든 인터페이스 메서드 구현 필수
}