오버라이딩(overring) - 메서드 재정의

일상 코딩·2022년 8월 10일
post-thumbnail

01.하위 클래스에서 메서드 재정의 하기

  • 오버라이딩(overriding) : 상위 클래스에 정의된 메서드의 구현 내용이 하위 클래스에서 구현할 내용과 맞지 않는 경우 하위 클래스에서 동일한 이름의 메서드를 재정의 할 수 있다.
  • 현재 VIPCustomer 클래스의 calcPrice()메서드에는 할인율이 적용되지 않았다.
  • VIPCustomer클래스의 calcPrice()메서드를 재정의 하여 할인율을 구현해야 한다.

Customer.java

package ch04;

public class Customer {
	protected int customerID; 
	protected String customerName;
	protected String customerGrade;
	int bounusPoint;
	double bounusRatio;
	
	public Customer(int customerID, String customerName) {
		bounusRatio = 0.01;
		this.customerID = customerID;
		this.customerName = customerName;
	}
	
	public int getCustomerID() {
		return customerID;
	}

	public void setCustomerID(int customerID) {
		this.customerID = customerID;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getCustomerGrade() {
		return customerGrade;
	}

	public void setCustomerGrade(String customerGrade) {
		this.customerGrade = customerGrade;
	}

	public int calcPrice(int price) {
		bounusPoint += price * bounusRatio; // 보너스 포인트 적립
		return price;
	}
	
	public String showCustomerInfo() {
		return customerName + "님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bounusPoint + "입니다.";
	}
}

VIPCustomer.java

package ch04;

public class VIPCustomer extends Customer {
	int salesPrice;
	double salesRatio;
	private String agentID;

	public VIPCustomer(int customerID, String customerName) {
		super(customerID, customerName);
		
		customerGrade = "VIP";
		bounusRatio = 0.05;
		salesRatio = 0.1;
	}
	
	public String getAgent() {
		return agentID;
	}
	
	@Override // calcPrice 메서드 재정의
	public int calcPrice(int price) {
		bounusPoint += price * bounusRatio;
		price -= price * salesRatio; // 상품 가격 - 할인가
		return price; 
	}	
}

CustomerTest.java

package ch04;

public class CustomerTest {

	public static void main(String[] args) {
	
		// 일반 고객 1% 보너스 포인트 적립
		Customer customerLee = new Customer(10010, "이순신");
		customerLee.bounusPoint = 1000;
		int price = customerLee.calcPrice(1000);
		System.out.println(customerLee.showCustomerInfo() + " 지불금액은 " + price + "원 입니다.");
	
		// VIP 고객 5% 보너스 포인트 적립 & 상품가 10%  할인
		VIPCustomer customerKim = new VIPCustomer(10020, "김유신");
		customerKim.bounusPoint = 1000;
		price = customerKim.calcPrice(1000);
		System.out.println(customerKim.showCustomerInfo() + " 지불금액은 " + price + "원 입니다.");		
	}
}

출력 결과

  • 이처럼 상위 클래스에 정의된 메서드의 구현 내용이 하위 클래스에서 구현할 내용과 맞지 않는 경우 하위 클래스에서 상위 클래스의 메서드를 다시 재정의(overriding)할 수 있다.

02.@overriding 애노테이션 (annotation)

  • 애노테이션은 원래 주석이라는 의미
  • 컴파일러에게 특별한 정보를 제공해주는 역할

  • @overriding 애노테이션은 재정의 된 메서드라는 의미로 선언부가 기존의 메서드와 다른 경우 에러 발생

03.형 변환과 오버라이딩 메서드 호출

  • Customer vc = new VIPCustomer();
  • vc 변수의 타입은 Customer지만 인스턴스의 타입은 VIPCustomer 이다.
  • 자바에서는 항상 인스턴스의 메서드가 호출 된다. (가상메서드의 원리)
  • 자바의 모든 메서드는 가상 메서드(virtual method) 이다.

CustomerTest.java

package ch04;

public class CustomerTest {

	public static void main(String[] args) {
		
		// 일반 고객 1% 보너스 포인트 적립
		Customer customerLee = new Customer(10010, "이순신");
		customerLee.bounusPoint = 1000;
		int price = customerLee.calcPrice(1000);
		System.out.println(customerLee.showCustomerInfo() + " 지불금액은 " + price + "원 입니다.");
		
		// VIP 고객 5% 보너스 포인트 적립 & 상품가 10%  할인
		VIPCustomer customerKim = new VIPCustomer(10020, "김유신");
		customerKim.bounusPoint = 1000;
		price = customerKim.calcPrice(1000);
		System.out.println(customerKim.showCustomerInfo() + " 지불금액은 " + price + "원 입니다.");
		
		Customer vc = new VIPCustomer(12345, "no-name");
		System.out.println(vc.calcPrice(1000)); // 가상 메서드
	}
}

출력 결과

  • 자식 클래스에서 재정의 된 메서드가 호출된다.
profile
일취월장(日就月將) - 「날마다 달마다 성장하고 발전한다.」

0개의 댓글