java

25. 다형성 (Polymorphism)

dalc3154 2025. 2. 27. 17:23

다형성이란?

많은 형태를 가질 수 있는 특성

하나의 객체가 여러 타입의 인스턴스(객체)로 취급될 수 있는 특성

상속, 메소드 오버라이드, 클래스 타입 변환

 

instanceof 연산자

참조변수값 instanceof 타입(클래스명)

해당 객체가 특정 클래스의 인스턴스인지, 또는 그 클래스를 상속받은 자식 클래스의 인스턴스인지 확인하고

그 결과를 boolean값으로 반환한다

 

예시)

클래스를 만들고 상속

// instanceof: 운송수단
class Vehicle{
	void displayInfo() {
		System.out.println("운송수단입니다");
	}
}

class Bus extends Vehicle{
	@Override
	void displayInfo() {
		System.out.println("버스를 타고 갑니다");
	}
}

class Subway extends Vehicle{
	@Override
	void displayInfo() {
		System.out.println("지하철을 타고 갑니다");
	}
}

class Bird {
	void displayInfo() {
		System.out.println("새는 운송수단이 아닙니다");
	}
}

 

 

다형성을 적용

public class F_Polymorphism {
	public static void main(String[] args) {
		// 다형성 적용
		// 부모 클래스 타입의 참조 변수로 자식 클래스 객체를 참조
		Vehicle vehicle = new Vehicle();
		Vehicle bus = new Bus();
		Vehicle subway = new Subway();
		//Vehicle bird = new Bird();
		
		if (subway instanceof Vehicle) {
			// subway 객체가 Vehicle의 인스턴스 || Vehicle을 상속받은 인스턴스 일 경우
			subway.displayInfo();// 지하철을 타고 갑니다
		}
		if (bus instanceof Vehicle) {
			// bus 객체가 Vehicle의 인스턴스 || Vehicle을 상속받은 인스턴스 일 경우
			bus.displayInfo();// 버스를 타고 갑니다
		}
		if (vehicle instanceof Bus) {
			System.out.println("운송수단은 버스입니다");
		} else {
			System.out.println("운송수단은 버스가 아닙니다");// 운송수단은 버스가 아닙니다
		}
		// instanceof 연산자가 true를 반환하는 경우
		// 해당 클래스 타입으로 형 변환이 가능
	}
}

'java' 카테고리의 다른 글

27. 인터페이스(Interface)  (0) 2025.02.27
26. 추상화(Abstract)  (0) 2025.02.27
24. 클래스의 형 변환  (0) 2025.02.25
실습) 도서 관리 시스템  (0) 2025.02.25
23. final  (0) 2025.02.25