Java/레퍼런스 형변환
슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출
Bohemian life
2012. 4. 13. 23:44
package com.cast3;//슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 class Parent{ public void parentPrn(){ System.out.println("슈퍼 클래스 : parentPrn 메서드"); } } class Child extends Parent{ //메서드 오버라이딩(재정의) public void parentPrn(){ System.out.println("서브 클래스 : 오버라이딩된 parentPrn 메서드"); } public void childPrn(){ System.out.println("서브 클래스 : ChildPrn 메서드"); } } public class RefTest06 { public static void main(String[] args){ Child c = new Child(); c.parentPrn(); Parent p; p=c; //자식 클래스 타입 -> 부모 클래스 타입 //업 캐스팅, 자동형변환 p.parentPrn(); //오버라이딩 되면은 원래 되로 출력되지 않고 서브 클래스가 출력된다 //형변환이 되었더라도 } }