Programming/Java

[Java] overloading μ΄λž€? μ˜€λ²„λ‘œλ”© 뜻

yuri lee 2022. 11. 1. 22:07
λ°˜μ‘ν˜•
이 글은 μƒν™œμ½”λ”©μ˜ 'Java' κ°•μ’Œλ₯Ό λ°”νƒ•μœΌλ‘œ μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€.

 

같은 μ΄λ¦„μ΄μ§€λ§Œ μ„œλ‘œ λ‹€λ₯Έ λ§€κ°œλ³€μˆ˜μ˜ ν˜•μ‹μ„ 가진 λ©”μ†Œλ“œλ₯Ό μ—¬λŸ¬κ°œ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

class Calculator{
    int left, right;
    int third = 0;
      
    public void setOprands(int left, int right){
        System.out.println("setOprands(int left, int right)");
        this.left = left;
        this.right = right;
    }
     
    public void setOprands(int left, int right, int third){
        System.out.println("setOprands(int left, int right, int third)");
        this.left = left;
        this.right = right;
        this.third = third;
    }
     
    public void sum(){
        System.out.println(this.left+this.right+this.third);
    }
      
    public void avg(){
        System.out.println((this.left+this.right+this.third)/3);
    }
}
  
public class CalculatorDemo {
      
    public static void main(String[] args) {
          
        Calculator c1 = new Calculator();
        c1.setOprands(10, 20);
        c1.sum();       
        c1.avg();
        c1.setOprands(10, 20, 30);
        c1.sum();       
        c1.avg();
         
    }
  
}

// output
// setOprands(int left, int right)
// 30
// 10
// setOprands(int left, int right, int third)
// 60
// 20
this.left = left;
this.right = right;

이 뢀뢄이 μ€‘λ³΅λ˜λ―€λ‘œ μ•„λž˜μ™€ 같이 μ½”λ“œλ₯Ό μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 

class Calculator{
    int left, right;
    int third = 0;
      
    public void setOprands(int left, int right){
        System.out.println("setOprands(int left, int right)");
        this.left = left;
        this.right = right;
    }
     
    public void setOprands(int left, int right, int third){
        this.setOprands(left, right);
        System.out.println("setOprands(int left, int right, int third)");
        this.third = third;
    }
     
    public void sum(){
        System.out.println(this.left+this.right+this.third);
    }
      
    public void avg(){
        System.out.println((this.left+this.right+this.third)/3);
    }
}
  
public class CalculatorDemo {
      
    public static void main(String[] args) {
          
        Calculator c1 = new Calculator();
        c1.setOprands(10, 20);
        c1.sum();       
        c1.avg();
        c1.setOprands(10, 20, 30);
        c1.sum();       
        c1.avg();
         
    }
  
}

// output
// setOprands(int left, int right)
// 30
// 10
// setOprands(int left, int right)
// setOprands(int left, int right, int third)
// 60
// 20

이λ₯Ό ν†΅ν•΄μ„œ μ•Œ 수 μžˆλŠ” 것은 λ§€κ°œλ³€μˆ˜μ˜ μˆ«μžμ— λ”°λΌμ„œ 같은 μ΄λ¦„μ˜, μ„œλ‘œ λ‹€λ₯Έ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κ³  μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

이름은 κ°™μ§€λ§Œ μ‹œκ·Έλ‹ˆμ²˜λŠ” λ‹€λ₯Έ λ©”μ†Œλ“œλ₯Ό μ€‘λ³΅μœΌλ‘œ μ„ μ–Έ ν•  수 μžˆλŠ” 방법을 λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©(overloading)이라고 ν•©λ‹ˆλ‹€.

 

μ˜€λ²„λ‘œλ”©μ˜ κ·œμΉ™

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ€ λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 즉 λ§€κ°œλ³€μˆ˜κ°€ λ‹€λ₯΄λ©΄ 이름이 같아도 μ„œλ‘œ λ‹€λ₯Έ λ©”μ†Œλ“œκ°€ λ˜λŠ” κ²ƒμž…λ‹ˆλ‹€. λ°˜λ©΄μ— λ§€κ°œλ³€μˆ˜λŠ” κ°™μ§€λ§Œ λ¦¬ν„΄νƒ€μž…μ΄ λ‹€λ₯΄λ©΄ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. μ•„λž˜μ˜ μ½”λ“œλ₯Ό λ΄…μ‹œλ‹€.

public class OverloadingDemo {
    void A (){System.out.println("void A()");}
    void A (int arg1){System.out.println("void A (int arg1)");}
    void A (String arg1){System.out.println("void A (String arg1)");}
    //int A (){System.out.println("void A()");}
    public static void main(String[] args) {
        OverloadingDemo od = new OverloadingDemo();
        od.A();
        od.A(1);
        od.A("coding everybody");
    }
}

// output
// void A()
// void A (int arg1)
// void A (String arg1)

 

λ©”μ†Œλ“œμ˜ 이름은 κ°™μ•„μ•Ό ν•˜κ³ , λ§€κ°œλ³€μˆ˜λŠ” 달라야 ν•˜μ§€λ§Œ 리턴값은 κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€. 

 

3ν–‰κ³Ό 4ν–‰μ˜ λ©”μ†Œλ“œ AλŠ” λ§€κ°œλ³€μˆ˜μ˜ μˆ«μžκ°€ λ‹€λ¦…λ‹ˆλ‹€. 4ν–‰κ³Ό 5ν–‰μ˜ λ©”μ†Œλ“œ AλŠ” 인자의 μˆ«μžλŠ” κ°™μ§€λ§Œ λ§€κ°œλ³€μˆ˜μ˜ 데이터 νƒ€μž…μ΄ λ‹€λ¦…λ‹ˆλ‹€. 이런 κ²½μš°λŠ” μ˜€λ²„λ‘œλ”©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. λ©”μ†Œλ“œλ₯Ό 호좜 ν•  λ•Œ μ „λ‹¬λ˜λŠ” 인자의 데이터 νƒ€μž…μ— λ”°λΌμ„œ μ–΄λ–€ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν• μ§€λ₯Ό μžλ°”κ°€ νŒλ‹¨ ν•  수 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ λ©”μ†Œλ“œμ˜ λ°˜ν™˜κ°’μ€ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” μ‹œμ μ—μ„œ μ „λ‹¬λ˜μ§€ μ•ŠλŠ” 정보이기 λ•Œλ¬Έμ— μ˜€λ²„λ‘œλ”©μ˜ λŒ€μƒμ΄ 될 수 μ—†μŠ΅λ‹ˆλ‹€.

상속과 μ˜€λ²„λ‘œλ”©

class OverloadingDemo {
    void A (){System.out.println("void A()");}
    void A (int arg1){System.out.println("void A (int arg1)");}
    void A (String arg1){System.out.println("void A (String arg1)");}
}


public class OverloadingDemo2 extends OverloadingDemo{
    void A (String arg1, String arg2){System.out.println("sub class : void A (String arg1, String arg2)");}
    void A (){System.out.println("sub class : void A ()");}
    public static void main(String[] args) {
        OverloadingDemo2 od = new OverloadingDemo2();
        od.A();
        od.A(1);
        od.A("coding everybody");
        od.A("coding everybody", "coding everybody");
         
    }
}

// output
// sub class : void A ()
// void A (int arg1)
// void A (String arg1)
// sub class : void A (String arg1, String arg2)

클래슀 OverloadingDemo2λŠ” OverloadingDemo을 상속 λ°›κ³  μžˆμŠ΅λ‹ˆλ‹€. OverloadingDemo2의 3ν–‰μ—μ„œ μ •μ˜λœ λ©”μ†Œλ“œ AλŠ” λ¬Έμžμ—΄μ„ λ°μ΄ν„°νƒ€μž…μœΌλ‘œ ν•˜λŠ” λ‘κ°œμ˜ λ§€κ°œλ³€μˆ˜λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ ν˜•νƒœμ˜ λ³€μˆ˜λŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œλŠ” μ •μ˜λ˜μ–΄ μžˆμ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ”©μ΄ λ˜λŠ” κ²ƒμž…λ‹ˆλ‹€. λ°˜λ©΄μ— 4ν–‰μ—μ„œ μ •μ˜λœ λ©”μ†Œλ“œ AλŠ” λ§€κ°œλ³€μˆ˜κ°€ μ—†μŠ΅λ‹ˆλ‹€. 그리고 λΆ€λͺ¨ ν΄λž˜μŠ€μ—λŠ” 이미 λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” λ©”μ†Œλ“œ Aκ°€ μ‘΄μž¬ν•©λ‹ˆλ‹€. 이 λ‘˜μ€ λ§€κ°œλ³€μˆ˜μ˜ ν˜•νƒœκ°€ κ°™κΈ° λ•Œλ¬Έμ— μ˜€λ²„λ‘œλ”©μ΄ μ•„λ‹ˆλΌ μ˜€λ²„λΌμ΄λ”©μ— ν•΄λ‹Ήν•©λ‹ˆλ‹€.

 

overriding VS overloading

μ˜€λ²„λΌμ΄λ”©κ³Ό μ˜€λ²„λ‘œλ”©μ€ μš©μ–΄κ°€ 참으둜 ν—·κ°ˆλ¦½λ‹ˆλ‹€. μ€‘μš”ν•œ 것은 μ˜€λ²„λΌμ΄λ”©μ΄ 무엇이고 μ˜€λ²„λ‘œλ”©μ΄ 무엇인가λ₯Ό κ΅¬λΆ„ν•˜λŠ” 것은 μ•„λ‹™λ‹ˆλ‹€. riding(μ˜¬λΌνƒ„λ‹€)을 μ΄μš©ν•΄μ„œ λΆ€λͺ¨ 클래슀의 λ©”μ†Œλ“œμ˜ λ™μž‘λ°©λ²•μ„ λ³€κ²½ν•˜κ³ , loading을 μ΄μš©ν•΄μ„œ 같은 이름, λ‹€λ₯Έ λ§€κ°œλ³€μˆ˜μ˜ λ©”μ†Œλ“œλ“€μ„ μ—¬λŸ¬κ°œ λ§Œλ“€ 수 μžˆλ‹€λŠ” 사싀을 μ•„λŠ” 것이 μ€‘μš”ν•©λ‹ˆλ‹€. λ‹€λ§Œ ν•™μŠ΅μ΄λ‚˜ ν˜‘μ—…μ˜ κ³Όμ •μ—μ„œ κ°œλ…μ„ μ£Όκ³  받을 λ•ŒλŠ” μš©μ–΄κ°€ μ€‘μš”ν•΄μ§‘λ‹ˆλ‹€. overλ₯Ό μ œμ™Έν•˜κ³  μ•Œμ•„λ‘λ©΄ 덜 ν—·κ°ˆλ¦¬μ§€ μ•Šμ„κΉŒ μ‹ΆμŠ΅λ‹ˆλ‹€λ‹€. (참고둜 overridingλŠ” μž¬μ •μ˜λΌλŠ” 사전적인 μ˜λ―Έκ°€ μžˆμŠ΅λ‹ˆλ‹€.)

λ°˜μ‘ν˜•

'Programming > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Java] overriding μ˜€λ²„λΌμ΄λ”©μ΄λž€?  (0) 2022.10.25
[Java] 상속과 μƒμ„±μž  (0) 2022.10.25
[Java] 상속  (0) 2022.10.24
[Java] μ΄ˆκΈ°ν™”μ™€ μƒμ„±μž  (0) 2022.10.24