Programming/Java

[Java] 상속과 μƒμ„±μž

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

 

상속과 μƒμ„±μž

package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
    public static void main(String[] args) {
        ConstructorDemo  c = new ConstructorDemo();
    }
}

μœ„ μ½”λ“œλŠ” μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ConstructorDemo 객체λ₯Ό 생성할 λ•Œ μžλ™μœΌλ‘œ μƒμ„±μžλ₯Ό λ§Œλ“€μ–΄μ£ΌκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. 

 

package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
    public ConstructorDemo(int param1) {}
    public static void main(String[] args) {
        ConstructorDemo  c = new ConstructorDemo();
    }
}

ν•˜μ§€λ§Œ μœ„ μ½”λ“œλŠ” μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€. λ§€κ°œλ³€μˆ˜κ°€ μžˆλŠ” μƒμ„±μžκ°€ μžˆμ„ λ•ŒλŠ” μžλ™μœΌλ‘œ κΈ°λ³Έ μƒμ„±μžλ₯Ό λ§Œλ“€μ–΄μ£Όμ§€ μ•ŠκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. 즉 μœ„ μ½”λ“œλŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 

 

package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
    public ConstructorDemo(){}
    public ConstructorDemo(int param1) {}
    public static void main(String[] args) {
        ConstructorDemo  c = new ConstructorDemo();
    }
}

λ”°λΌμ„œ κΈ°λ³Έ μƒμ„±μžλ₯Ό μΆ”κ°€ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. μ–΄λ– ν•œ μƒμ„±μžλ“  μ •μ˜ν•˜λ©΄, μžλ°”λŠ” κΈ°λ³Έ μƒμ„±μžλ₯Ό μžλ™μœΌλ‘œ λ§Œλ“€μ–΄μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 

 

class Calculator {
    int left, right;

    public Calculator(){}
     
    public Calculator(int left, int right){
        this.left = left;
        this.right = 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 SubstractionableCalculator(int left, int right) {
        this.left = left;
        this.right = right;
    }
 
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorConstructorDemo5 {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator(10, 20);
        c1.sum();
        c1.avg();
        c1.substract();
    }
}

μƒμœ„ ν΄λž˜μŠ€μ— κΈ°λ³Έ μƒμ„±μžλ₯Ό λ§Œλ“œλŠ” 것도 λ°©λ²•μ΄μ§€λ§Œ, κΈ°λ³Έ μƒμ„±μžλ₯Ό λ§Œλ“€μ§€ μ•Šκ³ λ„ 문제 없이 객체가 λ™μž‘ν•˜λ„λ‘ ν•  수 μžˆλŠ” 방법이 μžˆμŠ΅λ‹ˆλ‹€. ν˜„μž¬ μƒμœ„ 클래슀의 μƒμ„±μžμ™€ ν•˜μœ„ 클래슀의 μƒμ„±μžμ˜ λ‘œμ§μ€ κ°™μŠ΅λ‹ˆλ‹€. ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ λ˜‘κ°™μ€ 일을 ν•˜κ³  있기 λ•Œλ¬Έμ— 쀑볡이라고 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μƒμœ„ 클래슀의 같은 μ½”λ“œλ₯Ό λ³΅μ‚¬ν•˜λŠ” λŒ€μ‹ μ—, μƒμœ„ 클래슀의 μƒμ„±μžλ₯Ό ν•˜μœ„ ν΄λž˜μŠ€κ°€ 생성될 λ–„ ν˜ΈμΆœν•΄μ„œ μ‹€ν–‰μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. 

 

super

superλŠ” μƒμœ„ 클래슀λ₯Ό κ°€λ¦¬ν‚€λŠ” ν‚€μ›Œλ“œ μž…λ‹ˆλ‹€.

package org.opentutorials.javatutorials.Inheritance.example3;
 
class Calculator {
    int left, right;
     
    public Calculator(){}
     
    public Calculator(int left, int right){
        this.left = left;
        this.right = 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 SubstractionableCalculator(int left, int right) {
        super(left, right);
    }
 
    public void substract() {
        System.out.println(this.left - this.right);
    }
}
 
public class CalculatorConstructorDemo5 {
    public static void main(String[] args) {
        SubstractionableCalculator c1 = new SubstractionableCalculator(10, 20);
        c1.sum();
        c1.avg();
        c1.substract();
    }
}

// output
// 30
// 15
// -10

ν•˜μœ„ 클래슀의 μƒμ„±μžμ—μ„œ superλ₯Ό μ‚¬μš©ν•  λ•Œ μ£Όμ˜ν•  점은 superκ°€ κ°€μž₯ λ¨Όμ € λ‚˜νƒ€λ‚˜μ•Ό ν•œλ‹€λŠ” μ ‘μž…λ‹ˆλ‹€. 즉 λΆ€λͺ¨κ°€ μ΄ˆκΈ°ν™”λ˜κΈ° 전에 μžμ‹μ΄ μ΄ˆκΈ°ν™”λ˜λŠ” 일을 λ°©μ§€ν•˜κΈ° μœ„ν•œ 정책이라고 μƒκ°ν•˜λ©΄ λ©λ‹ˆλ‹€. 

λ°˜μ‘ν˜•