티스토리 뷰
예시코드
class A{
  int a;
}
class B extends A{
  int b;
}
class C extends A{
  int c;
}
class D extends A{
  int d;
}
public class ClassCastingTest1 {
  public static void main(String[] args) {
    A a = new B(); //업캐스팅
    B b = (B)a; // 다운캐스팅
    B[] listB = new B[10];
    C[] listC = new C[10];
    D[] listD = new D[10];
    A[] listA = new A[10];
    listA[0] = new C();
    listA[1] = new D();
    listA[2] = new B();
    // listA[2].b = 10;
    // 이건 안 보인다. 부모 자체가 A이니까.
    // 그래서 b로 형변환 해서 써줘야 한다는 것.
    ((B)listA[2]).b = 10;
    B b1 = (B)listA[2];
    if(listA[1] instanceof D) {
      D d1 = (D)listA[1];
    }
    
    System.out.println(b1.b); //10 츨력 (형변환 전에는 0나왔을 터)
  }
}
A라는 부모, B C D는 자식클래스
A a = new B(); // 업캐스팅
부모에 자식이 들어가는 것(낡은 껍데기에 신형 엔진)
즉, 자바 컴파일러는 타입을 보고 어떤 기능을 쓸 지를 결정한다.
B b = (B) a; // 다운캐스팅
신형 껍데기에 낡은 엔진이 들어가는 건 위험하므로, 억지로 끼워맞춰줌.
    B[] listB = new B[10]; 
    C[] listC = new C[10]; 
    D[] listD = new D[10]; 
    A[] listA = new A[10]; 
    listA[0] = new C(); 
    listA[1] = new D(); 
    listA[2] = new B();
부모 배열에 자식 배열 요소 다 담을 수 있다.
-----------
(B)listA[2] 이거는 다운캐스팅
타입이 A이고, 실제 들어간 건 B니까.
즉, 부모 타입에 자식이 들어간 것.
listA[2].b 이거 자체가 낡은 껍데기에 신형 엔진을 쓰려고 하는 것.
그리고 우선순위가 . 이 더 높다.
따라서 일부러 다운캐스팅 하면서 ()을 한 번 더 쳐준 거다.
예시코드 2
class Country{
  String hello(String name) {
    System.out.println("Con");
    return "Con"+name;
  }
}
class Eng extends Country{
  String hello(String name) {
    System.out.println("Eng");
    return "Eng"+name;
  }
}
class Kor extends Country {
  String hello(String name) {
    System.out.println("Kor");
    return "Kor"+name;
  }
}
class Jpn extends Country {
  String hello(String name) {
    System.out.println("Jpn");
    return "Jpn"+name;
  }
}
public class OverridingTest {
  public static void main(String[] args) {
    Country c = new Kor();
    
	System.out.print(c.hello("한국"));
    
  }
}
지금 이거는 이렇게 된다.
class Country{
  String hello(String name) {
    System.out.println("Con");
    return "Con"+name;
  }
}
class Eng extends Country{
  String hello(String name) {
    System.out.println("Eng");
    return "Eng"+name;
  }
}
class Kor extends Country {
  String hello(String name) {
    System.out.println("Kor");
    return "Kor"+name;
  }
}
class Jpn extends Country {
  String hello(String name) {
    System.out.println("Jpn");
    return "Jpn"+name;
  }
}
public class OverridingTest {
  public static void main(String[] args) {
    Country c = new Kor();
//    System.out.println(c.hello("한국"));
    // 이건 이렇게 분리 가능
    String result = c.hello("한국");
    System.out.println(result);
    /*
    지금 보면 hello()에도 print가 있다.
    Kor로 객체를 만들었기 때문에(다형성의 묘미)
    print 한 번 불려서 Kor 이 한 번 출력되고,
    리턴값인 Kor + name(한국) 이 출력되는 것임.
     */
  }
}
728x90
    
    
  반응형
    
    
    
  '[개발] - Java > Mega' 카테고리의 다른 글
| <보충> Day25, equals() 개념 (0) | 2023.05.04 | 
|---|---|
| 함수 유의사항 + 물 가져오기 문제 유의사항 (0) | 2023.05.04 | 
| <보충> upCasting과 downCasting (1) | 2023.05.03 | 
| Day23. super 보충 (0) | 2023.04.30 | 
| <보충> Day28-29 파일 입출력 (0) | 2023.04.30 | 
 
                            Comments