Assembled by GimunLee (2019-11-19)
- Upcastingκ³Ό Downcastingμ λν΄ μ€λͺ ν μ μλ€.
μΊμ€ν (casting) μ΄λ νμ μ λ³ννλ κ²μ λ§νλ©° νλ³ν μ΄λΌκ³ λ ν©λλ€. μλ°μ μμ κ΄κ³μ μλ λΆλͺ¨μ μμ ν΄λμ€ κ°μλ μλ‘ κ°μ νλ³νμ΄ κ°λ₯ν©λλ€.
μ΄λ² κΈμμλ μμ ν΄λμ€κ° λΆλͺ¨ ν΄λμ€μ νμ μΌλ‘ μΊμ€ν λλ μ μΊμ€ν κ³Ό λ°λλ‘ λΆλͺ¨ ν΄λμ€κ° μμ ν΄λμ€μ νμ μΌλ‘ μΊμ€ν λλ λ€μ΄μΊμ€ν μ λν΄μ μ 리ν©λλ€. μμνκΈ°μ μμ λΆλͺ¨ ν΄λμ€μΈ μμ κ΄κ³μ μμ ν΄λμ€λ₯Ό μνΌ ν΄λμ€ , κ·Έλ¦¬κ³ μμ ν΄λμ€μΈ νμ ν΄λμ€λ₯Ό μλΈ ν΄λμ€ λΌκ³ μ μν©λλ€.
κ°λ¨νκ² λ§νμλ©΄ μλ£νμ΄ μ ν΄μ§ λ³μμ κ°μ λ£μλλ, λ³μκ° μνλ μ 보λ₯Ό νλλ λΉ μ§ μμ΄ λ€ λ£μ΄μ€μΌ μ±λ¦½ν©λλ€.
μλ°μμ μλΈ ν΄λμ€λ μνΌ ν΄λμ€μ λͺ¨λ νΉμ±μ μμλ°μ΅λλ€. κ·Έλ κΈ° λλ¬Έμ μλΈ ν΄λμ€λ μνΌ ν΄λμ€λ‘ μ·¨κΈλ μ μμ΅λλ€. μ¬κΈ°μ 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 μ μμ μ κ³ μ ν νΉμ±μ μμ μλΈ ν΄λμ€μ κ°μ²΄λ₯Ό λ€μ 볡ꡬ μμΌμ£Όλ κ²μ λ§ν©λλ€. μ¦, 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
μ°μ°μκ° μμ΅λλ€.