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

[Java-기초] μΈν„°νŽ˜μ΄μŠ€ ν™œμš©ν•˜κΈ°

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 21.
λ°˜μ‘ν˜•

β–Ά ν•œ ν΄λž˜μŠ€κ°€ μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” 경우

ν•œ ν΄λž˜μŠ€κ°€ μ—¬λŸ¬ 클래슀λ₯Ό μƒμ†λ°›μœΌλ©΄ λ©”μ„œλ“œ 호좜이 λͺ¨ν˜Έν•΄μ§€λŠ” λ¬Έμ œκ°€ λ°œμƒν•  μˆ˜κ°€ μžˆλ‹€.

ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€λŠ” ν•œ ν΄λž˜μŠ€κ°€ μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•  수 μžˆλ‹€.

Buy μΈν„°νŽ˜μ΄μŠ€μ— 좔상 λ©”μ„œλ“œ buy( )κ°€ μ„ μ–Έλ˜μ–΄ 있고, Sell μΈν„°νŽ˜μ΄μŠ€μ— 좔상 λ©”μ„œλ“œ sell( )이 μ„ μ–Έλ˜μ–΄ μžˆλ‹€.

 

 

μΈν„°νŽ˜μ΄μŠ€λŠ” κ΅¬ν˜„ μ½”λ“œλ‚˜ 멀버 λ³€μˆ˜λ₯Ό 가지지 μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬λŸ¬ 개λ₯Ό λ™μ‹œμ— κ΅¬ν˜„ν•  수 μžˆλ‹€.

두 μΈν„°νŽ˜μ΄μŠ€μ— 이름이 같은 λ©”μ„œλ“œκ°€ μ„ μ–Έλ˜μ—ˆλ‹€κ³  해도 κ΅¬ν˜„μ€ ν΄λž˜μŠ€μ—μ„œ μ΄λ£¨μ–΄μ§€λ―€λ‘œ, μ–΄λ–€ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•˜λŠ”μ§€ λͺ¨ν˜Έν•˜μ§€ μ•Šμ€ 것이닀.

 

package interfaceex;

public class CustomerTest {
	public static void main(String[] args) {
		Customer customer = new Customer();

		Buy buyer = customer;  // Customer ν΄λž˜μŠ€ν˜•μΈ customerλ₯Ό Buy μΈν„°νŽ˜μ΄μŠ€ν˜•μΈ buyer에 λŒ€μž…ν•˜μ—¬ ν˜• λ³€ν™˜
		buyer.buy();  // buyerλŠ” Buy μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯
		buyer.order();

		Sell seller = customer;  // Customer ν΄λž˜μŠ€ν˜•μΈ customerλ₯Ό Sell μΈν„°νŽ˜μ΄μŠ€ν˜•μΈ seller에 λŒ€μž…ν•˜μ—¬ ν˜• λ³€ν™˜
		seller.sell();  // sellerλŠ” Sell μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯
		seller.order();

		if (seller instanceof Customer) {
			Customer customer2 = (Customer) seller;  // Sellerλ₯Ό ν•˜μœ„ ν΄λž˜μŠ€ν˜•μΈ Customer둜 λ‹€μ‹œ ν˜• λ³€ν™˜
			customer2.buy();
			customer2.sell();
		}
        
	}
}

 

 

 

β–Ά 두 μΈν„°νŽ˜μ΄μŠ€μ˜ λ””ν΄νŠΈ λ©”μ„œλ“œκ°€ μ€‘λ³΅λ˜λŠ” 경우

정적 λ©”μ„œλ“œλŠ” μΈμŠ€ν„΄νŠΈ 생성과 상관없이 μ‚¬μš©ν•  수 μžˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ””ν΄νŠΈ λ©”μ„œλ“œλŠ” μΈμŠ€ν„΄μŠ€λ₯Ό 생성해야 ν˜ΈμΆœν•  수 μžˆλŠ” λ©”μ„œλ“œμ΄κΈ° λ•Œλ¬Έμ—, 이름이 같은 λ””ν΄νŠΈ λ©”μ„œλ“œκ°€ 두 μΈν„°νŽ˜μ΄μŠ€μ— 있으면 λ¬Έμ œκ°€ λœλ‹€.

​

두 μΈν„°νŽ˜μ΄μŠ€ λͺ¨λ‘ λ””ν΄νŠΈ λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ©΄ Customer ν΄λž˜μŠ€μ—μ„œ 였λ₯˜κ°€ λ°œμƒν•œλ‹€. ​

 

​

λ””ν΄νŠΈ λ©”μ„œλ“œκ°€ μ€‘λ³΅λ˜μ—ˆμœΌλ‹ˆ 두 μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” Customer ν΄λž˜μŠ€μ— μž¬μ •μ˜ν•˜λΌλŠ” 뜻. Override둜 μž¬μ •μ˜ ν•΄μ£Όμž.

 

 

package interfaceex;

public class CustomerTest {
	public static void main(String[] args) {
		Customer customer = new Customer();

		Buy buyer = customer;
		buyer.buy();
		buyer.order();  // μž¬μ •μ˜λœ λ©”μ„œλ“œ 호좜됨

		Sell seller = customer;
		seller.sell();
		seller.order();  // μž¬μ •μ˜λœ λ©”μ„œλ“œ 호좜됨

		if (seller instanceof Customer) {
			Customer customer2 = (Customer) seller;
			customer2.buy();
			customer2.sell();
		}
		customer.order();  // μž¬μ •μ˜λœ λ©”μ„œλ“œ 호좜됨
	}
    
}

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

μ—¬κΈ°μ—μ„œ μ£Όμ˜ν•  점은 customerκ°€ Buyν˜•μœΌλ‘œ λ³€ν™˜λ˜κ³  buyer.order( )λ₯Ό ν˜ΈμΆœν•˜λ©΄ Buy에 κ΅¬ν˜„ν•œ λ””ν΄νŠΈ λ©”μ„œλ“œκ°€ μ•„λ‹Œ Customer ν΄λž˜μŠ€μ— μž¬μ •μ˜ν•œ λ©”μ„œλ“œκ°€ ν˜ΈμΆœλœλ‹€λŠ” 사싀이닀. μ΄λŠ” 254μͺ½μ˜ μƒμ†μ—μ„œ μ„€λͺ…ν•œ μžλ°” 가상 λ©”μ„œλ“œ 원리와 λ™μΌν•˜λ‹€.

​

 

 

β–Ά μΈν„°νŽ˜μ΄μŠ€ μƒμ†ν•˜κΈ°

μΈν„°νŽ˜μ΄μŠ€ 간에도 상속이 κ°€λŠ₯ν•˜λ‹€. μΈν„°νŽ˜μ΄μŠ€ κ°„ 상속은 κ΅¬ν˜„ μ½”λ“œλ₯Ό 톡해 κΈ°λŠ₯을 μƒμ†ν•˜λŠ” 것이 μ•„λ‹ˆλ―€λ‘œ ν˜• 상속이라고 λΆ€λ₯Έλ‹€. 클래슀의 κ²½μš°μ—λŠ” ν•˜λ‚˜μ˜ 클래슀만 상속받을 수 μžˆμ§€λ§Œ, μΈν„°νŽ˜μ΄μŠ€λŠ” μ—¬λŸ¬ 개λ₯Ό λ™μ‹œμ— 상속받을 수 μžˆλ‹€. ν•œ μΈν„°νŽ˜μ΄μŠ€κ°€ μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†λ°›μœΌλ©΄, 상속받은 μΈν„°νŽ˜μ΄μŠ€λŠ” μƒμœ„ μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έν•œ 좔상 λ©”μ„œλ“œλ₯Ό λͺ¨λ‘ κ°€μ§€κ²Œ λœλ‹€.

​

 

package interfaceex;

public interface MyInterface extends X, Y {
	void myMethod();
}

 

package interfaceex;

public class MyClass implements MyInterface {
	@Override
	public void x() {  // X μΈν„°νŽ˜μ΄μŠ€μ—μ„œ 상속받은 x() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("x()");
	}

	@Override
	public void y() {  // Y μΈν„°νŽ˜μ΄μŠ€μ—μ„œ 상속받은 y() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("y()");
	}

	@Override
	public void myMethod() {  // MyInterface μΈν„°νŽ˜μ΄μŠ€μ˜ myMethod() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("myMethod()");
	}
    
}

 

package interfaceex;

public class MyClassTest {
	public static void main(String[] args) {
		MyClass mClass = new MyClass();
		X xClass = mClass;  // μƒμœ„ μΈν„°νŽ˜μ΄μŠ€ Xν˜•μœΌλ‘œ λŒ€μž…ν•˜λ©΄
		xClass.x();  // X에 μ„ μ–Έν•œ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯

		Y yClass = mClass;  // μƒμœ„ μΈν„°νŽ˜μ΄μŠ€ Yν˜•μœΌλ‘œ λŒ€μž…ν•˜λ©΄
		yClass.y();  // Y에 μ„ μ–Έν•œ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯

		MyInterface iClass = mClass;  // κ΅¬ν˜„ν•œ μΈν„°νŽ˜μ΄μŠ€ν˜• λ³€μˆ˜μ— λŒ€μž…ν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€κ°€ μƒμ†ν•œ λͺ¨λ“  λ©”μ„œλ“œ 호좜 κ°€λŠ₯
		iClass.myMethod();
		iClass.x();
		iClass.y();
	}
    
}

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

μƒμ„±ν•œ ν΄λž˜μŠ€λŠ” μƒμœ„ μΈν„°νŽ˜μ΄μŠ€ν˜•μœΌλ‘œ λ³€ν™˜ν•  수 μžˆλ‹€. λ‹€λ§Œ μƒμœ„ μΈν„°νŽ˜μ΄μŠ€λ‘œ ν˜• λ³€ν™˜μ„ ν•˜λ©΄ μƒμœ„ μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έν•œ λ©”μ„œλ“œλ§Œ ν˜ΈμΆœν•  수 μžˆλ‹€.

예제λ₯Ό 보면 mClassκ°€ MyClass둜 μƒμ„±λ˜μ—ˆμ–΄λ„, X μΈν„°νŽ˜μ΄μŠ€ν˜•μœΌλ‘œ μ„ μ–Έν•œ xClass에 λŒ€μž…λ˜λ©΄ xClassκ°€ ν˜ΈμΆœν•  수 μžˆλŠ” λ©”μ„œλ“œλŠ” X의 λ©”μ„œλ“œμΈ x( )뿐이닀.

μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ •μ˜ν•  λ•Œ κΈ°λŠ₯상 계측 ꡬ쑰가 ν•„μš”ν•œ κ²½μš°μ— 상속을 μ‚¬μš©ν•˜κΈ°λ„ ν•œλ‹€.

λ°˜μ‘ν˜•

λŒ“κΈ€