- Do it! 자바 프로그래밍 입문 온라인 강의를 수강하며 작성하였습니다.
- Section 1. 자바의 핵심 - 객체지향 프로그래밍
- 24강 "오버라이딩과 다형성(3)"
- 메서드 오버라이딩(overridind) > 다형성 (polymorphism) > 다형성 활용하기
Customer 클래스(상위 클래스)와 VIPCustomer 클래스(하위 클래스)에 가격을 매개변수로 받는 calcPrice() 메서드를 이름은 동일하지만 return 값은 다르게 추가하여 확인해보겠다.
public class Customer {
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
}
public class VIPCustomer extends Customer{
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price - (int)(price * saleRatio);
}
}
main 함수에서 Customer 객체와 VIPCustomer 객체를 생성하여 출력해보면 VIPCustomer 의 메서드는 overriding 되어 VIPCustomer 클래스의 메서드가 적용된다.
public class OverridingTest {
public static void main(String[] args) {
Customer customerLee = new Customer(100010, "Lee");
int price1 = customerLee.calcPrice(10000);
customerLee.showCustomerInfo();
System.out.println(customerLee.getCustomerName() + "님의 지불 금액은 " + price1 + "입니다.");
VIPCustomer customerKim = new VIPCustomer(100020, "Kim", 100);
int price2 = customerKim.calcPrice(10000);
customerKim.showCustomerInfo();
System.out.println(customerKim.getCustomerName() + "님의 지불 금액은 " + price2 + "입니다.");
}
}
public class OverridingTest {
public static void main(String[] args) {
Customer customerPark = new VIPCustomer(100020, "Park", 100);
int price3 = customerPark.calcPrice(10000);
customerPark.showCustomerInfo();
System.out.println(customerPark.getCustomerName() + "님의 지불 금액은 " + price3 + "입니다.");
}
}
class Animal{
public void move() {
System.out.println("동물이 움직입니다.");
}
}
class Human extends Animal{
public void move() {
System.out.println("사람이 두발로 걷습니다.");
}
}
class Tiger extends Animal{
public void move() {
System.out.println("호랑이가 네발로 뜁니다.");
}
}
class Eagle extends Animal{
public void move() {
System.out.println("독수리가 하늘을 납니다.");
}
}
public class AnimalTest {
public static void main(String[] args) {
AnimalTest test = new AnimalTest();
test.moveAnimal(new Human());
test.moveAnimal(new Tiger());
test.moveAnimal(new Eagle());
}
public void moveAnimal(Animal animal) {
animal.move();
}
}
위 코드는 Animal 클래스를 상속받는 Human, Tiger, Eagle 클래스를 만들고
각각 move() 메서드를 통해 다른 메시지를 출력하도록 overriding 한 것이다.
Test 클래스에서는 moveAnimal() 메서드에서 Animal를 매개변수로 받으면 해당 객체의 move() 메서드를 호출하는 메서드를 만들었고, main 함수에서 각각 Human, Tiger, Eagle 인스턴스를 생성하여 매개변수로 moveAnimal에 넣어주었다.
moveAnimal() 메서드에서는 animal.move(); 라는 코드 한 줄이 쓰여있지만 실제 구현될 때는 여러 결과가 나타나게 되고, 이것을 다형성이라고 한다.
일단, GoldCustomer 클래스를 생성하여 Customer 클래스를 상속받도록 만들어준다.
public class GoldCustomer extends Customer{
public GoldCustomer(int customerID, String customerName) {
super(customerID, customerName);
customerGrade = "Gold";
bonusRatio = 0.05;
}
}
main 함수가 있는 Test 클래스에서는 다형성을 활용하기 위해 showInfoAndPrice() 메서드를 만들고 Customer 객체를 매개변수로 받는다.
public class OverridingTest {
public static void main(String[] args) {
OverridingTest test = new OverridingTest();
test.showInfoAndPrice(new Customer(1001, "customerLee"));
test.showInfoAndPrice(new VIPCustomer(1002, "customerKim", 100));
test.showInfoAndPrice(new GoldCustomer(1003, "customerPark"));
}
public void showInfoAndPrice(Customer customer) {
int price = customer.calcPrice(10000);
customer.showCustomerInfo();
System.out.println(customer.getCustomerName() + "님의 지불 금액은 " + price + "입니다.");
}
}
main함수에서 Customer, VIPCustomer, GoldCustomer 객체로 showInfoAndPrice() 메서드를 호출하면 결과가 각각 정상적으로 출력되는 것을 확인할 수 있다.