# λ‹€ν˜•μ„±(polymorphism)

written by sohyeon, hyemin πŸ’‘


# 1. λ‹€ν˜•μ„±μ΄λž€

객체지ν–₯κ°œλ…μ—μ„œ λ‹€ν˜•μ„±μ΄λž€ μ—¬λŸ¬ 가지 ν˜•νƒœλ₯Ό κ°€μ§ˆ 수 μžˆλŠ” λŠ₯λ ₯을 μ˜λ―Έν•œλ‹€.

μžλ°”μ—μ„œ μ‘°μƒν΄λž˜μŠ€ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μžμ†ν΄λž˜μŠ€μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μžˆλ„λ‘ ν•˜μ˜€λ‹€.

CaptionTV c = new CaptionTV();
TV t = new CaptionTV();

CaptionTV μΈμŠ€ν„΄μŠ€ 2개λ₯Ό μƒμ„±ν•˜κ³ , μ°Έμ‘°λ³€μˆ˜ c와 tκ°€ μƒμ„±λœ μΈμŠ€ν„΄μŠ€λ₯Ό ν•˜λ‚˜μ”© μ°Έμ‘°ν•˜λ„λ‘ ν•œλ‹€.

μ‹€μ œ μΈμŠ€ν„΄μŠ€κ°€ CaptionTV νƒ€μž…μ΄λΌ 할지라도, μ°Έμ‘°λ³€μˆ˜ tλ‘œλŠ” CaptionTV μΈμŠ€ν„΄μŠ€μ˜ λͺ¨λ“  멀버λ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.

λ‘˜ λ‹€ 같은 νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€μ§€λ§Œ μ°Έμ‘°λ³€μˆ˜μ˜ νƒ€μž…μ— 따라 μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ κ°œμˆ˜κ°€ 달라진닀.

# 이처럼 μ‘°μƒνƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μžμ†νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μžˆμ§€λ§Œ, μžμ†νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μ‘°μƒνƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μ—†λ‹€.

# 2. μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜

κΈ°λ³Έν˜• λ³€μˆ˜μ™€ 같이 μ°Έμ‘°λ³€μˆ˜λ„ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€. 단, μ„œλ‘œ 상속관계에 μžˆλŠ” ν΄λž˜μŠ€μ‚¬μ΄μ—μ„œλ§Œ κ°€λŠ₯ν•˜λ‹€.

μžμ†νƒ€μž… -> μ‘°μƒνƒ€μž…(Up-casting) : ν˜•λ³€ν™˜ μƒλž΅κ°€λŠ₯.   // μžμ†νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ₯Ό μ‘°μƒνƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ λ³€ν™˜ν•˜λŠ” 것
μžμ†νƒ€μž… <- μ‘°μƒνƒ€μž…(Down-casting) : ν˜•λ³€ν™˜ μƒλž΅λΆˆκ°€  // μ‘°μƒνƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ₯Ό μžμ†νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ λ³€ν™˜ν•˜λŠ” 것
  • ν˜•λ³€ν™˜μ€ μ°Έμ‘°λ³€μˆ˜μ˜ νƒ€μž…μ„ λ³€ν™˜ν•˜λŠ” 것이지 μΈμŠ€ν„΄μŠ€λ₯Ό λ³€ν™˜ν•˜λŠ” 것은 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜μ€ μΈμŠ€ν„΄μŠ€μ— μ•„λ¬΄λŸ° 영ν–₯을 λ―ΈμΉ˜μ§€ μ•ŠλŠ”λ‹€.

  • 단지 μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜μ„ ν†΅ν•΄μ„œ, μ°Έμ‘°ν•˜κ³  μžˆλŠ” μΈμŠ€ν„΄μŠ€μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ λ²”μœ„(개수)λ₯Ό μ‘°μ ˆν•˜λŠ” 것이닀.

# ex) 예제

class CastingTest1 {
    public static void main(String args[]) {
        Car car = null;                    // Carνƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜ carλ₯Ό μ„ μ–Έν•˜κ³  null둜 μ΄ˆκΈ°ν™”ν•œλ‹€.
        FireEngine fe = new FireEngine();  // FireEngine μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜κ³  FireEngine νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜κ°€ μ°Έμ‘°ν•˜λ„λ‘ ν•œλ‹€.
        FireEngine fe2 = null;

        fe.water();
        car = fe;                          // car = (Car)fe;μ—μ„œ ν˜•λ³€ν™˜μ΄ μƒλž΅λœ ν˜•νƒœλ‹€. feκ°€ μ°Έμ‘°ν•˜κ³  μžˆλŠ” μΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°€ car에 μ €μž₯λœλ‹€. 
        fe2 = (FireEngine)car;             // μžμ†νƒ€μž… <- μ‘°μƒνƒ€μž…, carμ—λŠ” FireEngineμΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°€ μ €μž₯λ˜μ–΄ μžˆμœΌλ―€λ‘œ fe2에도 FireEngineμΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°€ μ €μž₯λœλ‹€. carμ™€λŠ” 달리, fe2λŠ” FireEngineνƒ€μž…μ΄λ―€λ‘œ FireEngineμΈμŠ€ν„΄μŠ€μ˜ λͺ¨λ“  멀버듀을 μ‚¬μš©ν•  수 μžˆλ‹€.
        fe2.water();
    }
}

class Car {
    String color;
    int door;

    void drive() {
        System.out.println("drive, Brrrr~");    
    }
    void stop() {
        System.out.println("stop!!!");
    }
}

class FireEngine extends Car {
    void water() {
        System.out.println("water!!!");
    }
}

# μ‹€ν–‰κ²°κ³Ό

water!!!
water!!!

# ex) 예제

class CastingTest2 {
    public static void main(String args[]) {
        Car car = new Car();
        Car car2 = null;
        FireEngine fe = null;
    
        car.drive();
        fe = (FireEngine) car;  // μ»΄νŒŒμΌμ€ κ°€λŠ₯ν•˜μ§€λ§Œ μ‹€ν–‰ μ‹œ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.
        fe.drive();
        car2 = fe;
        car2.drive();
    }
}

# μ‹€ν–‰κ²°κ³Ό

drive, Brrrr~
java.lang.ClassCastException: Car
at CastingTest2.main(CastingTest2.java:8)
μ„œλ‘œ 상속관계에 μžˆλŠ” νƒ€μž…κ°„μ˜ ν˜•λ³€ν™˜μ€ μ–‘λ°©ν–₯으둜 자유둭게 μˆ˜ν–‰λ  수 μžˆμœΌλ‚˜, μ°Έμ‘°λ³€μˆ˜κ°€ κ°€λ¦¬ν‚€λŠ” μΈμŠ€ν„΄μŠ€μ˜ μžμ†νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜μ€ ν—ˆμš©λ˜μ§€ μ•ŠλŠ”λ‹€.
κ·Έλž˜μ„œ μ°Έμ‘°λ³€μˆ˜κ°€ κ°€λ¦¬ν‚€λŠ” μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ΄ 무엇인지 ν™•μΈν•˜λŠ” 것이 μ€‘μš”ν•˜λ‹€.

# Reference & Additional Resources

Last Updated: 12/5/2020, 11:36:44 AM