public static void main(String[] args) {
Parent p = new Parent();
Object obj = new Parent();
Parent p1 = (Parent)obj;
p1.method();
/*
instanceOf
: 상속받은 Object를 부모 클래스의 instance로 생성
ChildOne -> Parent
ChildTwo -> Parent
생성된 instance에 어떤 자식 클래스가 생성되었는지 판별 할 수 있는 제어자
*/
Parent arrPar[] = new Parent[3];
arrPar[0] = new ChildOne();
arrPar[1] = new ChildTwo();
arrPar[2] = new ChildOne();
for (int i = 0; i < arrPar.length; i++) {
arrPar[i].method();//over ride된 method
if (arrPar[i] instanceof ChildOne) {//맞다면 true가 나옴
ChildOne one = (ChildOne)arrPar[i];
one.oneMethod();//over ride된 method 가 아님
}
}
}
public class Parent/*부모클래스에는 extends Object가 항상 생략*/ {
public void method() {
System.out.println("Parent method()");
}
}
public class ChildOne extends Parent {
@Override
public void method() {
System.out.println("ChildOne Over Ride method()");
}
public void oneMethod() {
// TODO Auto-generated method stub
System.out.println("ChildOne oneMethod");
}
}
public class ChildTwo extends Parent {
@Override
public void method() {
// TODO Auto-generated method stub
System.out.println("ChildTwo Over Ride method()");
}
}