Java 클래스 다형성

LIM JAEHO·2022년 6월 30일
0

Java 학습

목록 보기
12/19

Polymorphism 다형성

다형성 이란 한 객체가 여러가지 타입을 가질 수 있는 성질이다.
부모 클래스 타입의 참조 변수로 자식 클래스의 인스턴스를 참조할 수 있다.

class Person {

}

class Student extends Person {

}

public class Test {
    public static void main(String[] args) {
        Person p1 = new Student();			// 가능
        // Student s1 = new Person();		// 불가능, error

        System.out.println(p1 instanceof Person);		// true
        System.out.println(p1 instanceof Student);		// true
    }
}

예제

0개의 댓글