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)할 수 있다.
@overriding애노테이션은 재정의 된 메서드라는 의미로 선언부가 기존의 메서드와 다른 경우 에러 발생
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)); // 가상 메서드 } }
출력 결과
- 자식 클래스에서 재정의 된 메서드가 호출된다.