Skip to content

Latest commit

Β 

History

History
132 lines (86 loc) Β· 5.02 KB

Upcasting & Downcasting.md

File metadata and controls

132 lines (86 loc) Β· 5.02 KB

Upcasting & Downcasting

Assembled by GimunLee (2019-11-19)


Goal

  • Upcastingκ³Ό Downcasting에 λŒ€ν•΄ μ„€λͺ…ν•  수 μžˆλ‹€.

Casting (κ°•μ œ ν˜•λ³€ν™˜, λͺ…μ‹œμ  ν˜•λ³€ν™˜)

μΊμŠ€νŒ…(casting) μ΄λž€ νƒ€μž…μ„ λ³€ν™˜ν•˜λŠ” 것을 λ§ν•˜λ©° ν˜•λ³€ν™˜ 이라고도 ν•©λ‹ˆλ‹€. μžλ°”μ˜ 상속 관계에 μžˆλŠ” λΆ€λͺ¨μ™€ μžμ‹ 클래슀 κ°„μ—λŠ” μ„œλ‘œ κ°„μ˜ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

이번 κΈ€μ—μ„œλŠ” μžμ‹ ν΄λž˜μŠ€κ°€ λΆ€λͺ¨ 클래슀의 νƒ€μž…μœΌλ‘œ μΊμŠ€νŒ…λ˜λŠ” μ—…μΊμŠ€νŒ…κ³Ό λ°˜λŒ€λ‘œ λΆ€λͺ¨ ν΄λž˜μŠ€κ°€ μžμ‹ 클래슀의 νƒ€μž…μœΌλ‘œ μΊμŠ€νŒ…λ˜λŠ” λ‹€μš΄μΊμŠ€νŒ…μ— λŒ€ν•΄μ„œ μ •λ¦¬ν•©λ‹ˆλ‹€. μ‹œμž‘ν•˜κΈ°μ— μ•žμ„œ λΆ€λͺ¨ 클래슀인 상속 κ΄€κ³„μ˜ μƒμœ„ 클래슀λ₯Ό 수퍼 클래슀 , 그리고 μžμ‹ 클래슀인 ν•˜μœ„ 클래슀λ₯Ό μ„œλΈŒ 클래슀 라고 μ •μ˜ν•©λ‹ˆλ‹€.

κ°„λ‹¨ν•˜κ²Œ λ§ν•˜μžλ©΄ μžλ£Œν˜•μ΄ 정해진 λ³€μˆ˜μ— 값을 λ„£μ„λ•ŒλŠ”, λ³€μˆ˜κ°€ μ›ν•˜λŠ” 정보λ₯Ό ν•˜λ‚˜λ„ 빠짐 없이 λ‹€ λ„£μ–΄μ€˜μ•Ό μ„±λ¦½ν•©λ‹ˆλ‹€.


Upcasting

μžλ°”μ—μ„œ μ„œλΈŒ ν΄λž˜μŠ€λŠ” 수퍼 클래슀의 λͺ¨λ“  νŠΉμ„±μ„ μƒμ†λ°›μŠ΅λ‹ˆλ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— μ„œλΈŒ ν΄λž˜μŠ€λŠ” 수퍼 클래슀둜 취급될 수 μžˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œ Upcasting μ΄λž€ μ„œλΈŒ 클래슀의 객체가 수퍼 클래슀 νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜λ˜λŠ” 것을 λ§ν•©λ‹ˆλ‹€.

즉, 수퍼 클래슀 레퍼런슀 λ³€μˆ˜κ°€ μ„œλΈŒ 클래슀둜 κ°μ²΄ν™”λœ μΈμŠ€ν„΄μŠ€λ₯Ό 가리킬 수 있게 λ©λ‹ˆλ‹€.

class Parent {
    String parentValue;

    public Parent(String parentValue) {
        this.parentValue = parentValue;
    }
    
    public void print() {
    	System.out.println("Parent 호좜");
    }
}

class Child extends Parent {
    String childValue;

    public Child(String parentValue) {
        super(parentValue);
    }
    
    @Override
    public void print() {
    	System.out.println("Child 호좜");
    }
}

public class UpcastingTest {
    public static void main(String[] args) {
        Child child = new Child("λΆ€λͺ¨κ°’ μ±„μš°κΈ°");
        child.childValue = "μžμ‹κ°’ μ±„μš°κΈ°"; // OK

        Parent parent = child;
        parent.parentValue = "λΆ€λͺ¨κ°’ μ±„μš°κΈ°"; // OK
        
        parent.childValue = "μžμ‹κ°’ μ±„μš°κΈ°"; // 컴파일 μ—λŸ¬ λ°œμƒ
        
        parent.print(); // "Child 호좜"이 좜λ ₯λ©λ‹ˆλ‹€.
    }
}

μœ„ μ½”λ“œμ—μ„œ parent 레퍼런슀 λ³€μˆ˜λŠ” Child 객체λ₯Ό 가리킀고 있으며 Parent type이기 λ•Œλ¬Έμ— μ˜€λ‘œμ§€ μžμ‹ μ˜ ν΄λž˜μŠ€μ— μ†ν•œ λ©€λ²„λ§Œ 접근이 ν—ˆμš©λ©λ‹ˆλ‹€. λ”°λΌμ„œ childValue λ©€λ²„λŠ” Child type의 λ©€λ²„μ΄λ―€λ‘œ 컴파일 μ‹œμ μ— 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

이처럼 μ—…μΊμŠ€νŒ…μ„ ν•˜κ²Œλ˜λ©΄ 객체 내에 μžˆλŠ” λͺ¨λ“  멀버에 μ ‘κ·Όν•  수 μ—†μŠ΅λ‹ˆλ‹€. 였직 수퍼 클래슀의 λ©€λ²„μ—λ§Œ 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€. μ΄λŠ” ν•„λ“œλΏλ§Œ μ•„λ‹ˆλΌ λ©”μ„œλ“œ(Method)에도 λ™μΌν•˜κ²Œ μ μš©λ©λ‹ˆλ‹€.

μœ„μ˜ μ½”λ“œμ—μ„œμ²˜λŸΌ μ—…μΊμŠ€νŒ… μ‹œμ—λŠ” μ•„λž˜μ™€ 같이 λͺ…μ‹œμ μΈ νƒ€μž… μΊμŠ€νŒ… 선언을 ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€. μ„œλΈŒ 클래슀 ChildλŠ” Parent νƒ€μž…μ΄κΈ° λ•Œλ¬Έμ—λ„ κ·Έλ ‡μŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ Upcasting을 μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” λ‹€ν˜•μ„±(Polymorphism) κ³Ό 관련이 μžˆλŠ”λ°, λ‹€λ₯Έ μž₯μ—μ„œ μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.


Downcasting

Downcasting 은 μžμ‹ μ˜ κ³ μœ ν•œ νŠΉμ„±μ„ μžƒμ€ μ„œλΈŒ 클래슀의 객체λ₯Ό λ‹€μ‹œ 볡ꡬ μ‹œμΌœμ£ΌλŠ” 것을 λ§ν•©λ‹ˆλ‹€. 즉, Upcasting 된 것을 λ‹€μ‹œ μ›μƒνƒœλ‘œ λŒλ¦¬λŠ” 것을 λ§ν•©λ‹ˆλ‹€.

class Parent {
    String parentValue;

    public Parent(String parentValue) {
        this.parentValue = parentValue;
    }
}

class Child extends Parent {
    String childValue;

    public Child(String parentValue) {
        super(parentValue);
    }
}

public class DowncastingTest {
    public static void main(String[] args) {
        Parent parent = new Child("λΆ€λͺ¨κ°’ μ±„μš°κΈ°"); // Upcasting

        Child child = (Child) parent; // Downcasting

        child.parentValue = "λΆ€λͺ¨κ°’ μ±„μš°κΈ°"; // OK

        child.childValue = "μžμ‹κ°’ μ±„μš°κΈ°"; // OK
    }
}
Java

μ—¬κΈ°μ„œ μ—…μΊμŠ€νŒ…κ³Ό λ‹€λ₯Έ 점은 λͺ…μ‹œμ μœΌλ‘œ νƒ€μž…μ„ 지정해야 ν•œλ‹€λŠ” μ μž…λ‹ˆλ‹€. 그리고 μ—…μΊμŠ€νŒ…μ΄ 선행이 λ˜μ–΄μ•Ό ν•˜λŠ”λ°μš”. λ‹€μš΄μΊμŠ€νŒ…μ„ ν•˜λ©΄μ„œ ν˜•λ³€ν™˜ν•  λŒ€μƒμ„ μ§€μ •ν–ˆμ§€λ§Œ λ¬΄λΆ„λ³„ν•œ λ‹€μš΄μΊμŠ€νŒ…μ€ 컴파일 μ‹œμ μ—λŠ” 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•Šμ§€λ§Œ λŸ°νƒ€μž„ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚¬ κ°€λŠ₯성이 μžˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ μ•„λž˜μ™€ 같이 μ§„ν–‰ν•˜λŠ” κ²½μš°λŠ” 싀행쀑 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

Child child = (Child) new Parent("λΆ€λͺ¨κ°’ μ±„μš°κΈ°");

λ”°λΌμ„œ ν˜•λ³€ν™˜ν•  νƒ€μž…μ„ λͺ…μ‹œν•¨μœΌλ‘œμ„œ 컴파일 였λ₯˜λŠ” μ‚¬λΌμ‘Œμ§€λ§Œ μ‹€μ œ μ½”λ“œλ₯Ό μˆ˜ν–‰ν•˜λ©΄ λŸ°νƒ€μž„ μ‹œ, ClassCastException이 λ°œμƒν•˜κ²Œ λ©λ‹ˆλ‹€.

μ΄λ ‡κ²Œ ν˜Όλ™λ˜λŠ” 객체λ₯Ό κ΅¬λ³„ν•˜κΈ° μœ„ν•΄ 도움을 μ£ΌλŠ” instanceof μ—°μ‚°μžκ°€ μžˆμŠ΅λ‹ˆλ‹€.


Reference & Additional Resources