CS 곡뢀

λ‹€ν˜•μ„±(Polymorphism)μ΄λž€?

은진 2021. 10. 28. 21:03

OOP(객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°)의 4가지 νŠΉμ§• 쀑 ν•˜λ‚˜

  • 좔상화, μΊ‘μŠν™”, μΌλ°˜ν™” 관계, λ‹€ν˜•μ„±

 

λ‹€ν˜•μ„±(Polymorphism)

  • μ„œλ‘œ λ‹€λ₯Έ 클래슀의 객체가 같은 λ©”μ‹œμ§€λ₯Ό λ°›μ•˜μ„ λ•Œ 각자의 λ°©μ‹μœΌλ‘œ λ™μž‘ν•˜λŠ” λŠ₯λ ₯
  • μ˜€λ²„λΌμ΄λ”©(Overriding), μ˜€λ²„λ‘œλ”©(Overloading)
  • μ½”λ“œλ₯Ό κ°„κ²°ν•˜κ²Œ ν•  뿐 μ•„λ‹ˆλΌ 변화에도 μœ μ—°ν•˜κ²Œ λŒ€μ²˜ν•  수 μžˆλ‹€.
  • ꡬ체적으둜 ν˜„μž¬ μ–΄λ–€ 클래슀 객체가 μ°Έμ‘°λ˜λŠ”μ§€μ™€ λ¬΄κ΄€ν•˜κ²Œ ν”„λ‘œκ·Έλž˜λ°μ„ ν•  수 μžˆλ‹€.

 

1. λ‹€ν˜•μ„±μ„ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 경우

public class Cat {
  public void meow(){ System.out.println("μ•Όμ˜Ή"); }
}
public class Dog {
  public void bark(){ System.out.println("멍멍"); }
}
public class Parrot {
  public void sing(){ System.out.println("μ•ˆλ…•"); }
}

 

public class Main {
  public static void main(String[] args) {
    Cat cat = new Cat();
    Dog dog = new Dog();
    Parrot parrot = new Parrot();
    // 애완동물 μ„Έ 마리의 μšΈμŒμ†Œλ¦¬ 호좜
    cat.meow(); dog.bark(); parrot.sing();
  }
}

 

 

2. λ‹€ν˜•μ„±μ„ μ‚¬μš©ν•œ 경우

// λΆ€λͺ¨ 클래슀
public abstract class Pet {
  public abstract void talk();
}
// μžμ‹ 클래슀
public class Cat extends Pet {
  public void talk(){ System.out.println("μ•Όμ˜Ή"); }
}
public class Dog extends Pet {
  public void talk(){ System.out.println("멍멍"); }
}
public class Parrot extends Pet {
  public void talk(){ System.out.println("μ•ˆλ…•"); }
}

 

public class Main {
  public static void main(String[] args) {
    Pet[] pets = { new Cat(), new Dog(), new Parrot() };
    // 애완동물 μ„Έ 마리의 μšΈμŒμ†Œλ¦¬ 호좜
    for (int i = 0; i < 3; i++){
      // μ‹€μ œ μ°Έμ‘°ν•˜λŠ” 객체에 따라 talk λ©”μ„œλ“œκ°€ μ‹€ν–‰λœλ‹€.
      pets.talk();
    }
  }
}

 

 

 

 

 

Overloading vs Overriding

  • 곡톡점 : 같은 μ΄λ¦„μ˜ λ‹€λ₯Έ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.

 

μ˜€λ²„λ‘œλ”©(Overloading)

  • 같은 μ΄λ¦„μ˜ ν•¨μˆ˜(λ©”μ„œλ“œ)λ₯Ό μ—¬λŸ¬κ°œ μ •μ˜ν•œλ‹€.
  • λ§€κ°œλ³€μˆ˜μ˜ μœ ν˜•κ³Ό 개수λ₯Ό λ‹€λ₯΄κ²Œ ν•˜μ—¬ λ‹€μ–‘ν•œ μœ ν˜•μ˜ ν˜ΈμΆœμ— μ‘λ‹΅ν•œλ‹€.
class OverloadingTest{
    //이름이 cat인 λ©”μ„œλ“œ
    void cat(){
        System.out.println("λ§€κ°œλ³€μˆ˜ μ—†μŒ");
    }
    
    //λ§€κ°œλ³€μˆ˜ intν˜•μ΄ 2개인 cat λ©”μ„œλ“œ
    void cat(int a, int b){
        System.out.println("λ§€κ°œλ³€μˆ˜ :"+a+", "+b);
    }
    
    //λ§€κ°œλ³€μˆ˜ Stringν˜•μ΄ ν•œ 개인 cat λ©”μ„œλ“œ
    void cat(String c){
        System.out.println("λ§€κ°œλ³€μˆ˜ : "+ c);
    }
}


public class OverTest {
    public static void main(String[] args) {
        //OverloadingTest 객체 생성
        OverloadingTest ot = new OverloadingTest();
        
        //λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” cat() 호좜
        ot.cat();
        
        //λ§€κ°œλ³€μˆ˜κ°€ intν˜• λ‘κ°œμΈ cat() 호좜
        ot.cat(20, 80);
        
        //λ§€κ°œλ³€μˆ˜κ°€ String ν•œκ°œμΈ cat() 호좜
        ot.cat("μ˜€λ²„λ‘œλ”© μ˜ˆμ œμž…λ‹ˆλ‹€."); 
    }
}

 

 

 

 

 

μ˜€λ²„λΌμ΄λ”©(Overriding)

  • μƒμœ„ ν΄λž˜μŠ€κ°€ 가지고 μžˆλŠ” λ©”μ„œλ“œλ„ ν•˜μœ„ 클래슀둜 μƒμ†λ˜μ–΄ ν•˜μœ„ ν΄λž˜μŠ€μ— μ‚¬μš©ν•œλ‹€.
  • ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•΄μ„œλ„ μ‚¬μš©ν•  수 μžˆλ‹€.
  • λ©”μ„œλ“œμ˜ 이름이 μ„œλ‘œ κ°™κ³ , λ§€κ°œλ³€μˆ˜κ°€ κ°™κ³ , λ°˜ν™˜ν˜•μ΄ 같은 경우, λΆ€λͺ¨ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλŠ” λ¬΄μ‹œν•˜κ³  μžμ‹ 클래슀의 λ©”μ„œλ“œ κΈ°λŠ₯을 μ‚¬μš©ν•œλ‹€.
class Woman{ //λΆ€λͺ¨ν΄λž˜μŠ€
    public String name;
    public int age;
    
    //info λ©”μ„œλ“œ
    public void info(){
        System.out.println("μ—¬μž"+name+"λ‹˜μ€ "+age+"μ‚΄μž…λ‹ˆλ‹€.");
    }
}

//Woman클래슀(λΆ€λͺ¨ν΄λž˜μŠ€)λ₯Ό μƒμ†λ°›μŒ :
class Job extends Woman{  
    String job;
    
    //λΆ€λͺ¨(Woman)ν΄λž˜μŠ€μ— μžˆλŠ” info()λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜
    public void info() {
        super.info();
        System.out.println("μ—¬μžμ˜ 직업은 "+job+"μž…λ‹ˆλ‹€.");
    }
}

public class OverTest {
    public static void main(String[] args) {    
        //Job 객체 생성
        Job job = new Job();
        
        //λ³€μˆ˜ μ„€μ •
        job.name = "유리";
        job.age = 30;
        job.job = "ν”„λ‘œκ·Έλž˜λ¨Έ";
        
        //호좜
        job.info(); 
    }
}

 

 

 

 

 

 

 

 

 

좜처: https://private.tistory.com/25 [κ³΅λΆ€ν•΄μ„œ 남 주자] 

https://gmlwjd9405.github.io/2018/07/05/oop-features.html

https://github.com/JaeYeopHan/Interview_Question_for_Beginner/tree/master/Java