Java/디딤돌 Java 언어 Part1

[Java 소스] 다형성 예

언제나휴일 2016. 12. 12. 00:21
반응형

[Java 소스] 다형성 예


Drummer.java

Musician.java

Pianist.java

Program.java



public class Musician {

    public void play(){

        System.out.println("랄라라");

    }

}



public class Pianist extends Musician{

    @Override public void play(){

        System.out.println("딩동댕");

    }

}


public class Drummer extends Musician{

    @Override public void play(){

        System.out.println("두두둥");

    }

}


public class Program {

    public static void main(String[] args){

        Musician musician = new Musician();

        musician.play();

        musician = new Pianist();

        musician.play();

        musician = new Drummer();

        musician.play();

    }

}


실행 결과

랄라라

딩동댕

두두둥



본문

[Java] 5. 2 다형성



반응형