다형성

coding by 스플릿·2021년 12월 29일
0

Java1

목록 보기
33/44

정의

  • 여러 가지 형태를 가질 수 있는 능력

사용

  • 조상클래스 타입의 참조변수로 자손클래스를 인스턴스를 생성할 수 있다
  • 조상 클래스에 있는 멤버만 사용할 수 있다.
  • 인터페이스 타입의 참조변수도 다형성으로 사용할 수 있다.
class Main{
    public static void main(String[] args) {
        smart_tv st1 = new smart_tv();
        tv st2 = new smart_tv();
        System.out.println(st1.text); //출력 : "Smart"
        System.out.println(st2.channel); //출력 : 0
        System.out.println(st2.text); //에러
    }
}
class tv{
    boolean power;
    int channel = 0;
}
class smart_tv extends tv{
    String text = "Smart";
    void play(){};
}

0개의 댓글