Programming/Java

[Java] overriding μ˜€λ²„λΌμ΄λ”©μ΄λž€?

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

 

창의적인 상속

상속은 μƒμœ„ 클래슀의 κΈ°λŠ₯을 ν•˜μœ„ ν΄λž˜μŠ€μ—κ²Œ λ¬Όλ €μ£ΌλŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€. κ·Έλ ‡λ‹€λ©΄ ν•˜μœ„ ν΄λž˜μŠ€λŠ” μƒμœ„ 클래슀의 λ©”μ†Œλ“œλ₯Ό 주어진 κ·ΈλŒ€λ‘œ μ‚¬μš©ν•΄μ•Ό ν• κΉŒμš”? λ§Œμ•½ κ·Έλž˜μ•Ό ν•œλ‹€λ©΄ μ œμ•½μ΄ 상당할 κ²ƒμž…λ‹ˆλ‹€. 이런 μ œμ•½μ„ λ²—μ–΄λ‚˜λ €λ©΄ ν•˜μœ„ ν΄λž˜μŠ€κ°€ λΆ€λͺ¨ 클래슀의 기본적인 λ™μž‘λ°©λ²•μ„ λ³€κ²½ν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 이런 λ§₯λ½μ—μ„œ λ„μž…λœ κΈ°λŠ₯이 λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©(overriding)μž…λ‹ˆλ‹€.

package org.opentutorials.javatutorials.overriding.example1;
 
class Calculator {
    int left, right;
 
    public void setOprands(int left, int right) {
        this.left = left;
        this.right = right;
    }
 
    public void sum() {
        System.out.println(this.left + this.right);
    }
 
    public void avg() {
        System.out.println((this.left + this.right) / 2);
    }
}
 
class SubstractionableCalculator extends Calculator {
     
    public void sum() {
        System.out.println("μ‹€ν–‰ κ²°κ³ΌλŠ” " +(this.left + this.right)+"μž…λ‹ˆλ‹€.");
    }
     
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorDemo {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator();
        c1.setOprands(10, 20);
        c1.sum();
        c1.avg();
        c1.substract();
    }
}

// μ‹€ν–‰ κ²°κ³ΌλŠ” 30μž…λ‹ˆλ‹€.
// 15
// -10

λ©”μ†Œλ“œ sum이  SubstractionableCalculator에 μΆ”κ°€ λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μ‹€ν–‰κ²°κ³ΌλŠ” c1.sum이 μƒμœ„ 클래슀의 λ©”μ†Œλ“œκ°€ μ•„λ‹ˆλΌ ν•˜μœ„ 클래슀의 λ©”μ†Œλ“œ sum을 μ‹€ν–‰ν•˜κ³  μžˆλ‹€λŠ” 것을 λ³΄μ—¬μ€λ‹ˆλ‹€. ν•˜μœ„ 클래슀 μž…μž₯μ—μ„œ λΆ€λͺ¨ ν΄λž˜μŠ€λž€ λ§ν•˜μžλ©΄ 기본적인 λ™μž‘ 방법을 μ •μ˜ν•œ 것이라고 생각할 수 μžˆμŠ΅λ‹ˆλ‹€. ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ μƒμ˜ ν΄λž˜μŠ€μ™€ λ™μΌν•œ λ©”μ†Œλ“œλ₯Ό μ •μ˜ν•˜λ©΄ λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° λ¬Όλ € 받은 κΈ°λ³Έ λ™μž‘ 방법을 λ³€κ²½ν•˜λŠ” 효과λ₯Ό κ°–κ²Œ λ©λ‹ˆλ‹€. κΈ°λ³Έλ™μž‘μ€ ν­λ„“κ²Œ 적용되고, μ˜ˆμ™Έμ μΈ λ™μž‘μ€ 더 높은 μš°μ„ μˆœμœ„λ₯Ό κ°–κ²Œν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 이것은 κ³΅ν•™μ—μ„œ 일반적으둜 λ°œκ²¬λ˜λŠ” κ·œμΉ™μž…λ‹ˆλ‹€. μ΄κ²ƒμ„ λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©(overriding)이라고 ν•©λ‹ˆλ‹€! 

 

μ˜€λ²„λΌμ΄λ”©μ˜ 쑰건

class Calculator {
    int left, right;
 
    public void setOprands(int left, int right) {
        this.left = left;
        this.right = right;
    }
 
    public void sum() {
        System.out.println(this.left + this.right);
    }
 
    public void avg() {
        System.out.println((this.left + this.right) / 2);
    }
}
 
class SubstractionableCalculator extends Calculator {
     
    public void sum() {
        System.out.println("μ‹€ν–‰ κ²°κ³ΌλŠ” " +(this.left + this.right)+"μž…λ‹ˆλ‹€.");
    }
    public int avg() {
        return (this.left + this.right)/2;
    }
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorDemo {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator();
        c1.setOprands(10, 20);
        c1.sum();
        c1.avg();
        c1.substract();
    }
}
./Playground/SubstractionableCalculator.java:9: error: avg() in SubstractionableCalculator cannot override avg() in Calculator
    public int avg() {
               ^
  return type int is not compatible with void
1 error

overriding을 ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ©”μ†Œλ“œμ˜ 리턴 ν˜•μ‹μ΄ κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€. 즉 클래슀 Calculator의 λ©”μ†Œλ“œ avgλŠ” 리턴 νƒ€μž…μ΄ voidμž…λ‹ˆλ‹€. 그런데 이것을 μƒμ†ν•œ 클래슀 SubstractionableCalculator의 리턴 νƒ€μž…μ€ intμž…λ‹ˆλ‹€. μ˜€λ²„λΌμ΄λ”©μ„ ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ•„λž˜μ˜ 쑰건을 μΆ©μ‘±μ‹œμΌœμ•Ό ν•©λ‹ˆλ‹€.

  • λ©”μ†Œλ“œμ˜ 이름
  • λ©”μ†Œλ“œ λ§€κ°œλ³€μˆ˜μ˜ μˆ«μžμ™€ 데이터 νƒ€μž… 그리고 μˆœμ„œ
  • λ©”μ†Œλ“œμ˜ 리턴 νƒ€μž…

μœ„μ™€ 같이 λ©”μ†Œλ“œμ˜ ν˜•νƒœλ₯Ό μ •μ˜ν•˜λŠ” 사항듀을 ν†΅ν„Έμ–΄μ„œ λ©”μ†Œλ“œμ˜ μ„œλͺ…(signature)라고 ν•©λ‹ˆλ‹€. 즉 μœ„μ˜ μ—λŸ¬λŠ” λ©”μ†Œλ“œλ“€ κ°„μ˜ μ„œλͺ…이 λ‹¬λΌμ„œ λ°œμƒν•œ λ¬Έμ œμž…λ‹ˆλ‹€. μ•„λž˜μ™€ 같이 μƒμœ„ 클래슀의 μ½”λ“œλ₯Ό λ³€κ²½ν•΄μ„œ 이 문제λ₯Ό ν•΄κ²°ν•΄λ΄…μ‹œλ‹€. 

class Calculator {
    int left, right;
 
    public void setOprands(int left, int right) {
        this.left = left;
        this.right = right;
    }
 
    public void sum() {
        System.out.println(this.left + this.right);
    }
 
    public int avg() {
        return (this.left + this.right)/2;
    }
}
 
class SubstractionableCalculator extends Calculator {
     
    public void sum() {
        System.out.println("μ‹€ν–‰ κ²°κ³ΌλŠ” " +(this.left + this.right)+"μž…λ‹ˆλ‹€.");
    }
    public int avg() {
        return (this.left + this.right)/2;
    }
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorDemo {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator();
        c1.setOprands(10, 20);
        c1.sum();
        c1.avg();
        c1.substract();
    }
}

// output
// μ‹€ν–‰ κ²°κ³ΌλŠ” 30μž…λ‹ˆλ‹€.
// -10

상속은 λΆ€λͺ¨μ˜ μˆ˜μ • 없이 μžμ‹μ˜ μˆ˜μ •μœΌλ‘œ λ³€κ²½ν•˜λŠ” 게 λͺ©μ μΈλ°, 이 μ˜ˆμ œλŠ” 쒋은 μ˜ˆμ œλŠ” μ•„λ‹™λ‹ˆλ‹€.

 

μƒμœ„ ν΄λž˜μŠ€μ™€ ν•˜μœ„ 클래슀의 μ„œλͺ…이 κ°™κΈ° λ•Œλ¬Έμ— λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©μ„ ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€. 그런데 μœ„μ˜ μ½”λ“œλ₯Ό 보면 쀑볡이 λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. λ©”μ†Œλ“œ avg의 λΆ€λͺ¨μ™€ μžμ‹ ν΄λž˜μŠ€κ°€ 같은 λ‘œμ§μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€. 쀑볡은 제거 λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. μƒμ„±μžμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ superλ₯Ό μ‚¬μš©ν•˜λ©΄ 이 문제λ₯Ό ν•΄κ²° ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

class Calculator {
    int left, right;
 
    public void setOprands(int left, int right) {
        this.left = left;
        this.right = right;
    }
 
    public void sum() {
        System.out.println(this.left + this.right);
    }
 
    public int avg() {
        return ((this.left + this.right) / 2);
    }
}
 
class SubstractionableCalculator extends Calculator {
     
    public void sum() {
        System.out.println("μ‹€ν–‰ κ²°κ³ΌλŠ” " +(this.left + this.right)+"μž…λ‹ˆλ‹€.");
    }
     
    public int avg() {
        return super.avg();
    }
     
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorDemo {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator();
        c1.setOprands(10, 20);
        c1.sum();
        System.out.println("μ‹€ν–‰ κ²°κ³ΌλŠ”" + c1.avg());
        c1.substract();
    }
}

μœ„ 클래슀의 λ©”μ†Œλ“œ avgμ—μ„œ μƒμœ„ 클래슀의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄μ„œ superλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 덕뢄에 μ½”λ“œμ˜ 쀑볡을 제거 ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œν•΄μ„œ λΆ€λͺ¨ 클래슀의 κΈ°λŠ₯을 λ³€κ²½ ν•  수 μžˆλŠ” 방법인 λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©μ— λŒ€ν•΄μ„œ μ•Œμ•„λ΄€μŠ΅λ‹ˆλ‹€~! 

λ°˜μ‘ν˜•

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

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