多形性Overrideの継承


public class People{
     
    public void printInfo() {
        System.out.println("나는 사람입니다.");
    }
}

public class Man extends People{
    @Override
    public void printInfo() {
        super.printInfo();
        System.out.println("그리고 전 남자입니다.");
    }
}

public class Woman extends People{
    @Override
    public void printInfo() {
        super.printInfo();
        System.out.println("그리고 전 여자입니다.");
    }
}

public class Seokhwi extends Man{
    @Override
    public void printInfo() {
        super.printInfo();
        System.out.println("그리고 전 seokhwi입니다");
    }
}

public class Test {
 
    public static void main(String[] args) {
        People people1=new People();
        people1.printInfo();

        People people2 = new Man();
        people2.printInfo();
         
        People people3 = new Man();
        people3.printInfo();

        BaeSeokHwi Seokhwi = new BaeSeokHwi();
        baeSeokHwi.printInfo();

        People[] arr = {people1,people2,people3,baeSeokHwi};
        for (People people : arr) {
            System.out.println("-----------------------------------------------");
            if(people instanceof People){
                System.out.println("is People Type");
            }
            if(people instanceof Man){
                System.out.println("is Man Type");
            }
            if(people instanceof Woman){
                System.out.println("is Woman Type");
            }
            if(people instanceof Seokhwi){
                System.out.println("is Seokhwi Type");

            }
        }        

         
    }
}

結果

나는 사람입니다.
나는 사람입니다.     
그리고 전 남자입니다.
나는 사람입니다.
그리고 전 남자입니다.
나는 사람입니다.
그리고 전 남자입니다.
그리고 전 배석휘입니다
-----------------------------------------------
is People Type
-----------------------------------------------
is People Type
is Man Type
-----------------------------------------------
is People Type
is Man Type
-----------------------------------------------
is People Type
is Man Type
is BaeSeokHwi Type