λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Language/Java

[Java] λ‹€ν˜•μ„±(polymorphism)μ΄λž€?

by jaee_ 2021. 8. 15.

πŸ“š λ©˜ν† λ‹˜μ΄ λ‹€ν˜•μ„±μ€ 무엇인지, μ™œ μ‚¬μš©ν•˜λŠ”μ§€μ— λŒ€ν•΄μ„œ λ¬Όμ–΄λ³΄μ…¨λŠ”λ° λ¨Έλ¦Ώμ†μœΌλ‘œλŠ” 뭔지 κ·Έλ €μ§€λŠ”λ° 막상 λŒ€λ‹΅μ„ ν•˜λ €λ‹ˆ λ‚΄κ°€ μ•„λŠ” λ‚΄μš©μ΄ λ§žλŠ”μ§€ μ˜μ‹¬μ΄ λ“€μ—ˆκ³ , ν˜„μž¬ μ•Œκ³ μžˆλŠ” λ‚΄μš©λ„ μ–΄λ–»κ²Œ μ„€λͺ…ν•΄μ•Όν•˜λŠ”μ§€ 말문이 ν„± λ§‰ν˜”λ‹€. μ•Œκ³ μžˆλ‹€κ³  μƒκ°ν–ˆμ§€λ§Œ λŒ€λ‹΅μ„ λͺ»ν–ˆλ˜,,^^  μžλ°”μ—μ„œ 맀우맀우 μ€‘μš”ν•œ  OOP(객체지ν–₯μ–Έμ–΄)의 νŠΉμ§• 쀑  ν•˜λ‚˜μΈ λ‹€ν˜•μ„±μ— λŒ€ν•΄μ„œ μ•Œμ•„λ³΄λ„λ‘ ν•˜μž!


λ‹€ν˜•μ„±(polymorphism)μ΄λž€?

객체지ν–₯κ°œλ…μ˜ μ€‘μš”ν•œ νŠΉμ§• μ€‘μ˜ ν•˜λ‚˜μΈ λ‹€ν˜•μ„±μ€ 'ν•˜λ‚˜μ˜ 객체가 μ—¬λŸ¬ 가지 νƒ€μž…μ„ κ°€μ§ˆ 수 μžˆλŠ” 것'을 μ˜λ―Έν•œλ‹€.  μžλ°”μ—μ„œλŠ” ν•œ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μ—¬λŸ¬ νƒ€μž…μ˜ κ°μ²΄λ₯Ό μ°Έμ‘°ν•  μˆ˜ μžˆλ„둝 ν•¨μœΌλ‘œμ¨ λ‹€ν˜•μ„±μ„ ν”„λ‘œκ·Έλž¨μ μœΌλ‘œ κ΅¬ν˜„ν–ˆλ‹€.
λ‹€ν˜•μ„±μ€ 상속, 좔상화와 λ”λΆˆμ–΄ 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ„ κ΅¬μ„±ν•˜λŠ” μ€‘μš”ν•œ νŠΉμ§• 쀑 ν•˜λ‚˜λ‹€.

 

λ‹€ν˜•μ„±μ˜ λŒ€ν‘œμ μΈ μ˜ˆλ‘œλŠ” μ˜€λ²„λ‘œλ”©, μ˜€λ²„λΌμ΄λ”©μ΄ μžˆλ‹€. μ˜€λ²„λ‘œλ”©κ³Ό μ˜€λ²„λΌμ΄λ”©μ— λŒ€ν•΄ μ •λ¦¬ν•œ λ‚΄μš©λ„ μžˆλ‹€. κΆκΈˆν•˜λ‹€λ©΄ μ•„λž˜μ— κ²Œμ‹œκΈ€μ„ 보길 λ°”λž€λ‹€. 

2021.08.11 - [Language/Java] - [Java] μ˜€λ²„λΌμ΄λ”©(Overriding)κ³Ό μ˜€λ²„λ‘œλ”©(Overloading)의 차이점

 

[Java] μ˜€λ²„λΌμ΄λ”©(Overriding)κ³Ό μ˜€λ²„λ‘œλ”©(Overloading)의 차이점

μ˜€λ²„λ‘œλ”©(Overloading)μ΄λž€?  λ©”μ†Œλ“œλ„ λ³€μˆ˜μ™€ 같이 같은 ν΄λž˜μŠ€λ‚΄μ—μ„œ ꡬ뢄이 ν•„μš”ν•˜κΈ° λ•Œλ¬Έμ— λ©”μ†Œλ“œ λͺ…이 각기 λ‹¬λΌμ•Όν•œλ‹€. ν•˜μ§€λ§Œ μžλ°”μ—μ„œλŠ” λ©”μ†Œλ“œλͺ…이 같더라도 λ§€κ°œλ³€μˆ˜μ˜ 개수 λ˜λŠ” νƒ€μž…

yeoonjae.tistory.com

 

μ½”λ“œλ₯Ό λ³΄λ©΄μ„œ λ‹€ν˜•μ„±μ„ μ΄ν•΄ν•΄λ³΄μž

// λΆ€λͺ¨ν΄λž˜μŠ€
public class Parent {
	void dowork() { System.out.println("λΆ€λͺ¨ 클래슀의 dowork() λ©”μ†Œλ“œμž…λ‹ˆλ‹€."); }
}

// μžμ‹ν΄λž˜μŠ€
public class Child extends Parent {
	void dowork() {
		System.out.println("μžμ‹ 클래슀의 dowork() λ©”μ†Œλ“œμž…λ‹ˆλ‹€.");
	}
}

// 예제클래슀
public class Polymorphism {
	// λ‹€ν˜•μ„±
	Parent parent = new Parent(); //(1)
	Child child = new Child(); //(2)

	Parent downCasting = (Parent) new Child(); //(3)
	Child childParent = new Parent(); //(4) -였λ₯˜λ°œμƒ
}

 

  • λΆ€λͺ¨ν΄λž˜μŠ€μΈ Parnetλ₯Ό μƒμ„±ν•œλ‹€.
  • μžμ‹ν΄λž˜μŠ€μΈ Childλ₯Ό μƒμ„±ν•˜κ³  Parentλ₯Ό μƒμ†λ°›λŠ”λ‹€.
  • Polymorphism클래슀λ₯Ό μƒμ„±ν•˜κ³  λΆ€λͺ¨ν΄λž˜μŠ€μ™€ μžμ‹ν΄λž˜μŠ€ λͺ¨λ‘ 객체λ₯Ό μƒμ„±ν•œλ‹€.
  • (1),(2)와 같이 같은 νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μžˆλ‹€.
  • (3)κ³Ό 같이 λΆ€λͺ¨ν΄λž˜μŠ€ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œλ„ μžμ‹ν΄λž˜μŠ€ νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μžˆλ‹€.
  • (4)와 같이 μžμ‹ν΄λž˜μŠ€ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ λΆ€λͺ¨ν΄λž˜μŠ€ νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•  수 μ—†λ‹€.

(1),(2) κ°€ κ°€λŠ₯ν•œ 이유

μ°Έμ‘° λ³€μˆ˜κ°€ μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ κ°œμˆ˜κ°€ μ‹€μ œ μΈμŠ€ν„΄μŠ€μ˜ 멀버 κ°œμˆ˜μ™€ κ°™κΈ° λ•Œλ¬Έ

 

(3)이 κ°€λŠ₯ν•œ 이유

μ°Έμ‘° λ³€μˆ˜κ°€ μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ κ°œμˆ˜κ°€ μ‹€μ œ μΈμŠ€ν„΄μŠ€μ˜ 멀버 κ°œμˆ˜λ³΄λ‹€ 적기 λ•Œλ¬Έ

 

(4)κ°€ μ°Έμ‘°ν•  수 μ—†λŠ” 이유

μ°Έμ‘° λ³€μˆ˜κ°€ μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ κ°œμˆ˜κ°€ μ‹€μ œ μΈμŠ€ν„΄μŠ€μ˜ 멀버 κ°œμˆ˜λ³΄λ‹€ 많기 λ•Œλ¬Έ

 

즉, ν΄λž˜μŠ€λŠ” 상속을 톡해 ν™•μž₯될 μˆ˜λŠ” μžˆμ–΄λ„ μΆ•μ†Œλ  μˆ˜λŠ” μ—†μœΌλ―€λ‘œ, μžμ‹ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” λ©€λ²„μ˜ κ°œμˆ˜κ°€ μ–Έμ œλ‚˜ λΆ€λͺ¨ν΄λž˜μŠ€μ™€ κ°™κ±°λ‚˜ 많기 λ•Œλ¬Έμ— μžμ‹->λΆ€λͺ¨λ₯Ό μ°Έμ‘°ν•  수 μ—†λ‹€. 

 


μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜

κΈ°λ³Έν˜• λ³€μˆ˜μ™€ 같이 μ°Έμ‘°λ³€μˆ˜λ„ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€. 단, μ„œλ‘œ 상속관계에 μžˆλŠ” 클래슀 μ‚¬μ΄μ—μ„œλ§Œ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— μ•„λž˜μ˜ λ°•μŠ€μ²˜λŸΌλ§Œ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€. 

μžμ‹ν΄λž˜μŠ€ -> λΆ€λͺ¨ν΄λž˜μŠ€ (UpCasting) : ν˜•λ³€ν™˜ μƒλž΅κ°€λŠ₯
λΆ€λͺ¨ν΄λž˜μŠ€ -> μžμ‹ν΄λž˜μŠ€ (DownCasting) : ν˜•λ³€ν™˜ μƒλž΅λΆˆκ°€

μ°Έμ‘°λ³€μˆ˜λ₯Ό μžμ‹->λΆ€λͺ¨ν΄λž˜μŠ€λ‘œ ν˜•λ³€ν™˜ν•˜λŠ” 것을 UpCasting(μ—…μΊμŠ€νŒ…)이라고 ν•˜λ©°, λΆ€λͺ¨->μžμ‹ν΄λž˜μŠ€λ‘œ ν˜•λ³€ν™˜ν•˜λŠ” 것을 DownCasting(λ‹€μš΄μΊμŠ€νŒ…)이라고 ν•œλ‹€. 

 

κ·Έλ ‡λ‹€λ©΄ ν˜•λ³€ν™˜μ„ ν•˜λŠ” 방법은 μ–΄λ–»κ²Œ 될까? μ•„λž˜μ˜ μ½”λ“œλ₯Ό 보자

// λΆ€λͺ¨ν΄λž˜μŠ€
public class Car {
	String color;
	int seat;
	
	void drive() {	// μš΄μ „κΈ°λŠ₯
		System.out.println("drive");
	}
	
	void stop() {	// λ©ˆμΆ”κΈ°
		System.out.println("stop!!!");
	}
	
}

// μžμ‹ν΄λž˜μŠ€
public class Sorento extends Car{
	void sunroof() {
		System.out.println("sunroof open~!");
	}
}

// μ‹€ν–‰ν΄λž˜μŠ€
public class Casting {
	public static void main(String[] args) {
		Car car = null;
		Sorento sorento = new Sorento();
		Sorento sorento2 = null;
		
		sorento.drive();
		sorento.sunroof();
		car = sorento;			//μ—…μΊμŠ€νŒ… (μƒλž΅λ˜μ–΄μžˆμŒ) μžμ‹ν΄λž˜μŠ€->λΆ€λͺ¨ν΄λž˜μŠ€
		
		sorento2 = (Sorento)car;	// λ‹€μš΄μΊμŠ€νŒ… λΆ€λͺ¨ν΄λž˜μŠ€->μžμ‹ν΄λž˜μŠ€
		sorento2.sunroof();
	}
}

μ‹€ν–‰κ²°κ³Ό

drive
sunroof open~!
sunroof open~!

instanceof μ—°μ‚°μž

μ΄λŸ¬ν•œ λ‹€ν˜•μ„±μœΌλ‘œ 인해 μ°Έμ‘°λ³€μˆ˜κ°€ μ‹€μ œλ‘œ μ°Έμ‘°ν•˜κ³  μžˆλŠ” μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ„ 확인할 ν•„μš”μ„±μ΄ 생긴닀. μžλ°”μ—μ„œλŠ” instanceofμ—°μ‚°μžλ₯Ό μ‚¬μš©ν•œλ‹€. μ—°μ‚°κ²°κ³Όλ‘œλŠ” boolean값인 true, falseλ₯Ό λ°˜ν™˜ν•˜λ©° trueλ₯Ό λ°˜ν™˜ν–ˆλ‹€λ©΄ μ°Έμ‘°λ³€μˆ˜κ°€ κ²€μ‚¬ν•œ νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€λŠ” 것을 λœ»ν•œλ‹€. 

μ°Έμ‘°λ³€μˆ˜ instanceof ν΄λž˜μŠ€μ΄λ¦„

예제

// λΆ€λͺ¨ν΄λž˜μŠ€
public class Car {//λ‚΄μš©μƒλž΅}

// μžμ‹ν΄λž˜μŠ€
public class Sorento extends Car{//λ‚΄μš©μƒλž΅}

// μ‹€ν–‰ν΄λž˜μŠ€
public class instanceofTest {
	public static void main(String[] args) {
		
		if(c instanceof Sorento) {
			System.out.println("This is a Sorento instance");
		}
		
		if(c instanceof Car) {
			System.out.println("This is a Car instance");
		}
		
		
		System.out.println(c.getClass().getName());	// 클래슀의 이름 좜λ ₯
		
	}

μ‹€ν–‰κ²°κ³Ό

This is a Sorento instance
This is a Car instance
polymorphism.Sorento

μœ„μ˜ μ½”λ“œμ²˜λŸΌ instanceof μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜λ©΄ μΈμŠ€ν„΄μŠ€μ˜ μ‹€μ œνƒ€μž…μ„ μ•Œ 수 μžˆλ‹€. 

 

μ–΄λ–€ νƒ€μž…μ— λŒ€ν•œ instanceof μ—°μ‚°μ˜ κ²°κ³Όκ°€ trueλΌλŠ” 것은 κ²€μ‚¬ν•œ νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€λŠ” 것을 λœ»ν•œλ‹€. 

 


μ°Έκ³ μ‚¬μ΄νŠΈ
http://tcpschool.com/java/java_polymorphism_concept
μžλ°”μ˜ μ‹ 
μžλ°”μ˜ 정석

 

λŒ“κΈ€