24강. 오버라이딩과 다형성(3)

철새·2022년 3월 1일
0
  • Do it! 자바 프로그래밍 입문 온라인 강의를 수강하며 작성하였습니다.
  • Section 1. 자바의 핵심 - 객체지향 프로그래밍
  • 24강 "오버라이딩과 다형성(3)"
  • 메서드 오버라이딩(overridind) > 다형성 (polymorphism) > 다형성 활용하기

메서드 오버라이딩(overridind)

  • 상위 클래스에 정의된 메서드 중 하위 클래스와 기능이 맞지 않거나 추가 기능이 필요한 경우 같은 이름과 매개변수로 하위 클래스에서 재정의 하는 것이다.

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 + "입니다.");
	}
}

가상 메서드 (virtual method)

  • 프로그램에서 어떤 객체의 변수나 메서드의 참조는 그 타입에 따라 이루어진다.
    (가상 메서드의 경우는 타입과 상관없이 실제 생성된 인스턴스의 메서드가 호출되는 원리)
  • customerPark의 타입은 Customer 이지만, 실제 생성된 인스턴스인 VIPCustomer 클래스의 calcPrice 메서드가 호출된다.

다형성 (polymorphism)

  • 하나의 코드가 여러가지 자료형으로 구현되어 실행되는 것
  • 정보은닉, 상속과 더불어 객체지향 프로그래밍의 가장 큰 특징 중 하나
  • 객체지향 프로그래밍의 유연성, 재활용성, 유지보수성에 기본이 되는 특징이다.

다형성 구현하기

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(); 라는 코드 한 줄이 쓰여있지만 실제 구현될 때는 여러 결과가 나타나게 되고, 이것을 다형성이라고 한다.

다형성 활용하기

  • 일반 고객과 VIP 고객의 중간 등급(Gold)의 고객을 생성
  • 5명의 고객을 ArrayList에 생성하여 저장한 다음 각 고객이 물건을 샀을 때의 가격과 보너스 포인트를 계산함

일단, 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() 메서드를 호출하면 결과가 각각 정상적으로 출력되는 것을 확인할 수 있다.

profile
효율성을 추구하며 세상을 떠도는 철새입니다.

0개의 댓글

관련 채용 정보