Notice
Recent Posts
Recent Comments
Β«   2024/12   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
관리 메뉴

A Joyful AI Research Journey🌳😊

[37] 230220 Java Ch. 8 μΈν„°νŽ˜μ΄μŠ€: 1. μΈν„°νŽ˜μ΄μŠ€, 2. νƒ€μž… λ³€ν™˜κ³Ό λ‹€ν˜•μ„± [K-디지털 νŠΈλ ˆμ΄λ‹ 37일] λ³Έλ¬Έ

🌳Bootcamp Revision 2023✨/Spring Framework, Java

[37] 230220 Java Ch. 8 μΈν„°νŽ˜μ΄μŠ€: 1. μΈν„°νŽ˜μ΄μŠ€, 2. νƒ€μž… λ³€ν™˜κ³Ό λ‹€ν˜•μ„± [K-디지털 νŠΈλ ˆμ΄λ‹ 37일]

yjyuwisely 2023. 2. 20. 12:32

230220 Mon 37th class

Ch. 8 μΈν„°νŽ˜μ΄μŠ€
진도: p. 370 ~ (ꡐ재: 혼자 κ³΅λΆ€ν•˜λŠ” μžλ°”, μ €μž: μ‹ μš©κΆŒ)
ν•œλΉ›λ―Έλ””μ–΄_혼곡자_μš©μ–΄μ§‘.pdf
μš©μ–΄ μ°Έκ³ 

 

혼자 κ³΅λΆ€ν•˜λŠ” μžλ°” | μ‹ μš©κΆŒ - ꡐ보문고

혼자 κ³΅λΆ€ν•˜λŠ” μžλ°” | 혼자 해도 μΆ©λΆ„ν•˜λ‹€! 1:1 κ³Όμ™Έν•˜λ“― λ°°μš°λŠ” μžλ°” ν”„λ‘œκ·Έλž˜λ° μžμŠ΅μ„œ (JAVA 8 &11 지원) 이 책은 λ…ν•™μœΌλ‘œ μžλ°”λ₯Ό λ°°μš°λŠ” μž…λ¬Έμžκ°€ ‘κΌ­ ν•„μš”ν•œ λ‚΄μš©μ„ μ œλŒ€λ‘œ’ ν•™μŠ΅ν•  수 μžˆλ„

product.kyobobook.co.kr

였늘 배운 것 쀑 κΈ°μ–΅ν•  것을 μ •λ¦¬ν–ˆλ‹€. 


μ§€λ‚œ μˆ˜μ—… λ•Œ 배운 것 쀑 λ‹€μ‹œ κΈ°μ–΅ν•  것

2023.02.17 - [1. Revision 2023/Java] - [36] 230217 Ch. 7 상속(3): 3. 좔상 클래슀

좔상 클래슀[λΆ€λͺ¨]: ν΄λž˜μŠ€λ“€μ˜ κ³΅ν†΅μ μΈ νŠΉμ„±μ„ μΆ”μΆœν•΄μ„œ μ„ μ–Έν•œ 클래슀
ν΄λž˜μŠ€λ“€μ˜ 곡톡적인 ν•„λ“œμ™€ λ©”μ„œλ“œλ₯Ό μΆ”μΆœν•΄μ„œ μ„ μ–Έν•œ ν΄λž˜μŠ€μ΄λ‹€.

싀체 클래슀[μžμ‹]: κ°μ²΄λ₯Ό 직접 생성할 수 μžˆλŠ” 클래슀 (μ΄λ•ŒκΉŒμ§€ 배운 것!) 
싀체 ν΄λž˜μŠ€λŠ” 좔상 클래슀의 λͺ¨λ“  νŠΉμ„±μ„ λ¬Όλ €λ°›κ³ , μΆ”가적인 νŠΉμ„±(ν•„λ“œμ™€ λ©”μ„œλ“œ)을 κ°€μ§ˆ 수 μžˆλ‹€. 

An argument is a way for you to provide more information to a function.


기타) 

API stands for 
Application Programming Interface
API = μ„€λͺ…μ„œ, μˆœμ„œ 같은 것 
An application programming interface(API) is a way for two or more computer programs to communicate with each other.

7 Examples of APIs in Use Today

  • Twitter Bots.
  • Log-In Using XYZ.
  • Weather Snippers.
  • Pay with PayPal.
  • Google Maps.
  • Travel Booking.
  • E-Commerce.
Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

https://docs.oracle.com/javase/8/docs/api/

ex) Math ν΄λ¦­ν–ˆμ„ λ•Œ λ‚˜μ˜€λŠ” Method Summaryλ₯Ό 보면 μ†ŒμŠ€λ₯Ό κ°€λ¦¬λŠ” μΈν„°νŽ˜μ΄μŠ€λ§Œ μ œκ³΅ν•΄μ€€λ‹€. 


Ch. 8 μΈν„°νŽ˜μ΄μŠ€

μ±…μ˜ λͺ©μ°¨
08-1 μΈν„°νŽ˜μ΄μŠ€
08-2 νƒ€μž… λ³€ν™˜κ³Ό λ‹€ν˜•μ„±

μΈν„°νŽ˜μ΄μŠ€(interface): 객체의 μ‚¬μš© 방법을 μ •μ˜ν•œ νƒ€μž…
개발 μ½”λ“œμ™€ 객체가 μ„œλ‘œ ν†΅μ‹ ν•˜λŠ” 접점 역할을 ν•œλ‹€. 
개발 μ½”λ“œκ°€ μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€λŠ” 객체의 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœμ‹œν‚¨λ‹€. ꡬ성 λ©€λ²„λŠ” μƒμˆ˜ ν•„λ“œμ™€ 좔상 λ©”μ„œλ“œμ΄λ‹€.

섀계 λͺ©μ μ΄ μ•„λ‹Œ κ΅¬ν˜„ λͺ©μ μœΌλ‘œ μ“΄λ‹€. 좔상 ν΄λž˜μŠ€λ³΄λ‹€ 더 μΆ”μƒμ μœΌλ‘œ ν•œλ‹€. 
κ΅¬ν˜„μ΄ μ•ˆ λ˜μ–΄ μžˆλŠ” λΆ€λͺ¨ 클래슀라고 μƒκ°ν•˜λ©΄ 이해가 쉽닀. 

Why And When To Use Interfaces?

1) To achieve security - hide certain details and only show the important details of an object (interface).

2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

μ°Έκ³ : https://www.w3schools.com/java/java_interface.asp

μΈν„°νŽ˜μ΄μŠ€(interface)λž€?

μžμ‹ ν΄λž˜μŠ€κ°€ μ—¬λŸ¬ λΆ€λͺ¨ 클래슀λ₯Ό 상속받을 수 μžˆλ‹€λ©΄, λ‹€μ–‘ν•œ λ™μž‘μ„ μˆ˜ν–‰ν•  수 μžˆλ‹€λŠ” μž₯점을 κ°€μ§€κ²Œ 될 κ²ƒμž…λ‹ˆλ‹€.
ν•˜μ§€λ§Œ 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ 닀쀑 상속을 ν•  경우 λ©”μ†Œλ“œ 좜처의 λͺ¨ν˜Έμ„± λ“± μ—¬λŸ¬ 가지 λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμ–΄ μžλ°”μ—μ„œλŠ” 클래슀λ₯Ό ν†΅ν•œ 닀쀑 상속은 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ 닀쀑 μƒμ†μ˜ 이점을 버릴 μˆ˜λŠ” 없기에 μžλ°”μ—μ„œλŠ” μΈν„°νŽ˜μ΄μŠ€λΌλŠ” 것을 ν†΅ν•΄ 닀쀑 상속을 μ§€μ›ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
μΈν„°νŽ˜μ΄μŠ€(interface)λž€ λ‹€λ₯Έ 클래슀λ₯Ό μž‘μ„±ν•  λ•Œ 기본이 λ˜λŠ” 틀을 μ œκ³΅ν•˜λ©΄μ„œ, λ‹€λ₯Έ 클래슀 μ‚¬μ΄μ˜ 쀑간 맀개 μ—­ν• κΉŒμ§€ λ‹΄λ‹Ήν•˜λŠ” μΌμ’…μ˜ 좔상 클래슀λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

μžλ°”μ—μ„œ 좔상 ν΄λž˜μŠ€λŠ” 좔상 λ©”μ†Œλ“œλΏλ§Œ μ•„λ‹ˆλΌ μƒμ„±μž, ν•„λ“œ, 일반 λ©”μ†Œλ“œλ„ 포함할 수 μžˆμŠ΅λ‹ˆλ‹€.
ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€(interface)λŠ” μ˜€λ‘œμ§€ 좔상 λ©”μ†Œλ“œμ™€ μƒμˆ˜λ§Œμ„ 포함할 수 μžˆμŠ΅λ‹ˆλ‹€.

Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class" that is used to group related methods with empty bodies:

// interface
interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void run(); // interface method (does not have a body)
}

Notes on Interfaces:

  • Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass)
  • Interface methods do not have a body - the body is provided by the "implement" class
  • On implementation of an interface, you must override all of its methods
  • Interface methods (좔상 λ©”μ„œλ“œ) are by default abstract and public
  • Interface attributes are by default public, static and final
  • An interface cannot contain a constructor (as it cannot be used to create objects)

8.1.1 μΈν„°νŽ˜μ΄μŠ€ μ„ μ–Έ

μΈν„°νŽ˜μ΄μŠ€ 선언은 class ν‚€μ›Œλ“œ λŒ€μ‹ μ— interface ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 

μžλ°”μ—μ„œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ„ μ–Έν•˜λŠ” 방법은 클래슀λ₯Ό μž‘μ„±ν•˜λŠ” 방법과 κ°™μŠ΅λ‹ˆλ‹€.
μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ„ μ–Έν•  λ•Œμ—λŠ” μ ‘κ·Ό μ œμ–΄μžμ™€ ν•¨κ»˜ interface ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.
μžλ°”μ—μ„œ μΈν„°νŽ˜μ΄μŠ€λŠ” λ‹€μŒκ³Ό 같이 μ„ μ–Έν•©λ‹ˆλ‹€.

μ ‘κ·Όμ œμ–΄μž interface μΈν„°νŽ˜μ΄μŠ€ 이름 {
    public static final νƒ€μž… μƒμˆ˜ 이름 = κ°’;
    ...
    public abstract λ©”μ„œλ“œ 이름 (λ§€κ°œλ³€μˆ˜ λͺ©λ‘);
    ...
}​


단, ν΄λž˜μŠ€μ™€λŠ” 달리 μΈν„°νŽ˜μ΄μŠ€μ˜ λͺ¨λ“  ν•„λ“œλŠ” public static final이어야 ν•˜λ©°, λͺ¨λ“  λ©”μ†Œλ“œλŠ” public abstract이어야 ν•©λ‹ˆλ‹€.
이 뢀뢄은 λͺ¨λ“  μΈν„°νŽ˜μ΄μŠ€μ— κ³΅ν†΅μœΌλ‘œ μ μš©λ˜λŠ” λΆ€λΆ„μ΄λ―€λ‘œ 이 μ œμ–΄μžλŠ” μƒλž΅ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
μ΄λ ‡κ²Œ μƒλž΅λœ μ œμ–΄μžλŠ” 컴파일 μ‹œ μžλ°” μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ μΆ”κ°€ν•΄ μ€λ‹ˆλ‹€.
class A{
// ν•„λ“œ (λ³€μˆ˜, μƒμˆ˜)
// μƒμ„±μž
// λ©”μ„œλ“œ
}

interface A{
// μƒμˆ˜ ν•„λ“œ
// μƒμ„±μž(X)
// 좔상 λ©”μ„œλ“œ
}

1) μƒμˆ˜ ν•„λ“œ (Constant field) μ„ μ–Έ

μΈν„°νŽ˜μ΄μŠ€μ˜ μƒμˆ˜ ν•„λ“œλŠ” κΈ°λ³Έμ μœΌλ‘œ public static final νŠΉμ„±μ„ 가진닀. κ΄€λ‘€μ μœΌλ‘œ ν•„λ“œ 이름은 λͺ¨λ‘ λŒ€λ¬Έμžλ‘œ μž‘μ„±ν•΄μ•Ό ν•˜λ©°, μ„ μ–Έ μ‹œ μ΄ˆκΈ°κ°’μ„ λŒ€μž…ν•΄μ•Ό ν•œλ‹€.
(static final μƒλž΅ κ°€λŠ₯)

[public static final] νƒ€μž… μƒμˆ˜μ΄λ¦„ = κ°’;

μƒμˆ˜ 이름은 λŒ€λ¬Έμžλ‘œ μž‘μ„±ν•˜λ˜, μ„œλ‘œ λ‹€λ₯Έ λ‹¨μ–΄λ‘œ κ΅¬μ„±λ˜μ–΄ μžˆμ„ κ²½μš°μ—λŠ” 언더바(_)둜 μ—°κ²°ν•˜λŠ” 것이 관둀이닀.
ex) MODEL, MAX_VALUE
μΈν„°νŽ˜μ΄μŠ€ μƒμˆ˜λŠ” λ°˜λ“œμ‹œ μ„ μ–Έκ³Ό λ™μ‹œμ— μ΄ˆκΈ°κ°’μ„ 지정해야 ν•œλ‹€.

μ˜ˆμ‹œ) μƒμˆ˜ ν•„λ“œ μ„ μ–Έ

public interface RemoteControl {
	//μƒμˆ˜ ν•„λ“œ
	//public static final int MAX_VALUE = 10;
	//public static final int MIN_VALUE = 0;
	public int MAX_VOLUME = 10;
	public int MIN_VOLUME = 0;
}

2) 좔상 λ©”μ„œλ“œ μ„ μ–Έ

μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ λ©”μ„œλ“œλŠ” public abstractκ°€ μƒλž΅λ˜κ³  λ©”μ„œλ“œ μ„ μ–ΈλΆ€λ§Œ μžˆλŠ” μΆ”상 λ©”μ„œλ“œμ΄λ‹€. κ΅¬ν˜„ ν΄λž˜μŠ€λŠ” λ°˜λ“œμ‹œ μΆ”상 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•΄μ•Όν•œλ‹€. 
(abstract μƒλž΅ κ°€λŠ₯)

[public abstract] λ¦¬ν„΄νƒ€μž… λ©”μ„œλ“œμ΄λ¦„(λ§€κ°œλ³€μˆ˜, ...);

μ˜ˆμ‹œ) 좔상 λ©”μ„œλ“œ μ„ μ–Έ

public interface RemoteControl {
	//μƒμˆ˜ ν•„λ“œ
	//public static final int MAX_VALUE = 10;
	//public static final int MIN_VALUE = 0;
	public int MAX_VOLUME = 10;
	public int MIN_VOLUME = 0;
	
	//좔상 λ©”μ„œλ“œ
	//public abstract void turnOn();
	//public abstract void turnOff();
	//public abstract void setVolume(int volume);
	
	public void turnOn();
	public void turnOff();
	public void setVolume(int volume);
}

8.1.2 μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

개발 μ½”λ“œκ°€ μΈν„°νŽ˜μ΄μŠ€ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€λŠ” 객체의 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œλ‹€.
κ°μ²΄λŠ” μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μ •μ˜λœ 좔상 λ©”μ„œλ“œμ™€ λ™μΌν•œ λ©”μ„œλ“œ 이름, 맀개 νƒ€μž…, 리턴 νƒ€μž…μ„ 가진 싀체 λ©”μ„œλ“œλ₯Ό 가지고 μžˆμ–΄μ•Ό ν•œλ‹€.
μ΄λŸ¬ν•œ 객체λ₯Ό μΈν„°νŽ˜μ΄μŠ€μ˜ κ΅¬ν˜„(implement) 객체라고 ν•˜κ³ , κ΅¬ν˜„ 객체λ₯Ό μƒμ„±ν•˜λŠ” 클래슀λ₯Ό κ΅¬ν˜„ 클래슀라고 ν•œλ‹€. 

implements: 
κ΅¬ν˜„ ν΄λž˜μŠ€λŠ” μ–΄λ–€ μΈν„°νŽ˜μ΄μŠ€λ‘œ μ‚¬μš© κ°€λŠ₯ν•œμ§€(μ–΄λ–€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜κ³  μžˆλŠ”지)λ₯Ό κΈ°μˆ ν•˜κΈ° μœ„ν•΄ ν΄λž˜μŠ€ μ„ μ–Έ μ‹œ implements ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class:

// Interface
interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

// Pig "implements" the Animal interface
class Pig implements Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    // The body of sleep() is provided here
    System.out.println("Zzz");
  }
}

class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}

1) κ΅¬ν˜„ 클래슀

public class κ΅¬ν˜„ν΄λž˜μŠ€ 이름 implements μΈν„°νŽ˜μ΄μŠ€μ΄λ¦„{
//μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ μ„ μ–Έ
}

μ˜ˆμ‹œ 1) κ΅¬ν˜„ 클래슀

public class Television implements RemoteControl {
	//ν•„λ“œ
	private int volume;
	
	//μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ 좔상 λ©”μ„œλ“œμ˜ κ΅¬ν˜„ λ©”μ„œλ“œ μ„ μ–Έ
	//turnOn() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void turnOn() {
		System.out.println("TVλ₯Ό μΌ­λ‹ˆλ‹€.");
	}
	
	//turnOff() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void turnOff() {
		System.out.println("TVλ₯Ό λ•λ‹ˆλ‹€.");
	}
	
	//setVolume() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void setVolume(int volume) {
		if(volume>RemoteControl.MAX_VOLUME) {
			this.volume = RemoteControl.MAX_VOLUME;
		} else if (volume<RemoteControl.MIN_VOLUME) {
			this.volume = RemoteControl.MIN_VOLUME;
		} else {
			this.volume = volume;
		}
		System.out.println("ν˜„μž¬ TV λ³Όλ₯¨: " + this.volume);
	}
}


μ˜ˆμ‹œ 2) κ΅¬ν˜„ 클래슀

public class Audio implements RemoteControl {
	//ν•„λ“œ
	private int volume;
	
	//μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ 좔상 λ©”μ„œλ“œμ˜ κ΅¬ν˜„ λ©”μ„œλ“œ μ„ μ–Έ
	//turnOn() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void turnOn() {
		System.out.println("Audioλ₯Ό μΌ­λ‹ˆλ‹€.");
	}
	
	//turnOff() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void turnOff() {
		System.out.println("Audioλ₯Ό λ•λ‹ˆλ‹€.");
	}
	
	//setVolume() 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ
	public void setVolume(int volume) {
		if(volume>RemoteControl.MAX_VOLUME) {
			this.volume = RemoteControl.MAX_VOLUME;
		} else if (volume<RemoteControl.MIN_VOLUME) {
			this.volume = RemoteControl.MIN_VOLUME;
		} else {
			this.volume = volume;
		}
		System.out.println("ν˜„μž¬  Audio λ³Όλ₯¨: " + this.volume);
	}
}

μΈν„°νŽ˜μ΄μŠ€λ‘œ κ΅¬ν˜„ 객체λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜κ³  κ΅¬ν˜„ 객체λ₯Ό λŒ€μž…ν•΄μ•Ό ν•œλ‹€.

μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜;
λ³€μˆ˜ = κ΅¬ν˜„κ°μ²΄;
μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜ = κ΅¬ν˜„κ°μ²΄;

예제 3) μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜μ— κ΅¬ν˜„ 객체 λŒ€μž…

public class RemoteControlExample {
	public static void main(String[] args) {
		//int a;
		RemoteControl rc;
		//a = 10;
		rc = new Television();
		//a = 20;
		rc = new Audio();
	}
}

2) 닀쀑 μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ 클래슀

μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑상속이 κ°€λŠ₯ν•˜λ‹€. (좔상 ν΄λž˜μŠ€λŠ” 닀쀑상속이 μ•ˆ λœλ‹€.)
μΈν„°νŽ˜μ΄μŠ€λŠ” μ†ŒμŠ€λ₯Ό κ°€λ¦¬λŠ” λͺ©μ μœΌλ‘œ λ³΄μ•ˆκ³Ό 연관이 μžˆλ‹€.
섀계, κ΅¬ν˜„ κ΅¬λΆ„μœΌλ‘œλ„ 쓰인닀. 

To implement multiple interfaces, separate them with a comma.

public class κ΅¬ν˜„ν΄λž˜μŠ€μ΄λ¦„ implements μΈν„°νŽ˜μ΄μŠ€A, μΈν„°νŽ˜μ΄μŠ€B{
	//μΈν„°νŽ˜μ΄μŠ€ A에 μ„ μ–Έλœ 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ μ„ μ–Έ
    //μΈν„°νŽ˜μ΄μŠ€ B에 μ„ μ–Έλœ 좔상 λ©”μ„œλ“œμ˜ 싀체 λ©”μ„œλ“œ μ„ μ–Έ
}

예제) 

interface FirstInterface {
  public void myMethod(); // interface method
}

interface SecondInterface {
  public void myOtherMethod(); // interface method
}

class DemoClass implements FirstInterface, SecondInterface {
  public void myMethod() {
    System.out.println("Some text..");
  }
  public void myOtherMethod() {
    System.out.println("Some other text...");
  }
}

class Main {
  public static void main(String[] args) {
    DemoClass myObj = new DemoClass();
    myObj.myMethod();
    myObj.myOtherMethod();
  }
}

닀쀑 μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•  경우, κ΅¬ν˜„ ν΄λž˜μŠ€λŠ” λͺ¨λ“  μΈν„°νŽ˜μ΄μŠ€μ˜ 좔상 λ©”μ„œλ“œμ— λŒ€ν•΄ 싀체 λ©”μ„œλ“œλ₯Ό μž‘μ„±ν•΄μ•Ό ν•œλ‹€.

예제 1) 인터넷을 검색할 수 μžˆλŠ” Searchable μΈν„°νŽ˜μ΄μŠ€
search() 좔상 λ©”μ„œλ“œ

public interface Searchable {
	//좔상 λ©”μ„œλ“œ
	//public abstract void search(String url);
	void search(String url); 
}


예제 2) 닀쀑 μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ 클래슀

public class SmartTelevision implements RemoteControl, Searchable{
	private int volume;
	
	//RemoteControl의 좔상 λ©”μ„œλ“œμ— λŒ€ν•œ 싀체 λ©”μ„œλ“œ
	@Override
	public void turnOn() {
		System.out.println("TVλ₯Ό μΌ­λ‹ˆλ‹€.");
	}

	@Override
	public void turnOff() {
		System.out.println("TVλ₯Ό λ•λ‹ˆλ‹€.");
	}

	@Override
	public void setVolume(int volume) {
		if(volume>RemoteControl.MAX_VOLUME) {
			this.volume = RemoteControl.MAX_VOLUME;
		} else if (volume<RemoteControl.MAX_VOLUME) {
			this.volume = RemoteControl.MIN_VOLUME;
		} else {
			this.volume = volume;
		}
		System.out.println("ν˜„μž¬ TV λ³Όλ₯¨: " + this.volume);
	}
	
	//Searchable의 좔상 λ©”μ„œλ“œμ— λŒ€ν•œ 싀체 λ©”μ„œλ“œ
	@Override
	public void search(String url) {
		System.out.println(url + "을 κ²€μƒ‰ν•©λ‹ˆλ‹€.");
	}
}


예제 3) μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜μ— κ΅¬ν˜„ 객체 λŒ€μž…

public class SmartTelevisionExample {
	public static void main(String[] args) {
		SmartTelevision tv = new SmartTelevision();
		
		RemoteControl rc = tv;
		Searchable searchable = tv;
	}
}

8.1.3 μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš© p. 379

κ΅¬ν˜„ 객체가 μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜μ— λŒ€μž…λœλ‹€λŠ” 것을 μ•Œμ•˜μœΌλ‹ˆ, μ§€κΈˆλΆ€ν„° μΈν„°νŽ˜μ΄μŠ€λ‘œ κ΅¬ν˜„ 객체λ₯Ό μ‚¬μš©ν•˜λŠ” 방법을 μ•Œμ•„λ³΄λ‚Ÿ. 
클래슀λ₯Ό μ„ μ–Έν•  λ•Œ μΈν„°νŽ˜μ΄μŠ€λŠ” ν•„λ“œ, μƒμ„±μž λ˜λŠ” λ©”μ„œλ“œμ˜ λ§€κ°œ λ³€μˆ˜, μƒμ„±μž λ˜λŠ” λ©”μ„œλ“œμ˜ λ‘œμ»¬ λ³€μˆ˜λ‘œ μ„ μ–Έλ  μˆ˜ μžˆλ‹€.

ex) λ‹€μŒμ€ RemoteControl μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν•„λ“œ, μƒμ„±μž λ˜λŠ” λ©”μ„œλ“œμ˜ 맀개 λ³€μˆ˜ 그리고 λ©”μ„œλ“œμ˜ 둜컬 λ³€μˆ˜λ‘œ μ„ μ–Έν–ˆλ‹€.

public class MyClass {
	//ν•„λ“œ
	//int a = 10;
	RemoteControl rc = new Television();
	
	//μƒμ„±μž
	MyClass(){}//κΈ°λ³Έ μƒμ„±μž(λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” μƒμ„±μž)
	MyClass(RemoteControl rc){//λ§€κ°œλ³€μˆ˜κ°€ ν•œ 개인 μƒμ„±μž
		this.rc = rc;
	}
	
	//λ©”μ„œλ“œ
	void method() {
		//둜컬 λ³€μˆ˜
	RemoteControl rc = new Audio();
	}
	
	void methodB(RemoteControl rc) {...}
}


예제 1) μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš© MyClass.java

package ch08;

public class MyClass {
	//ν•„λ“œ
	//int a = 10;
	RemoteControl rc = new Television();
	
	//μƒμ„±μž
	MyClass(){//κΈ°λ³Έ μƒμ„±μž(λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” μƒμ„±μž)
	}

	MyClass(RemoteControl rc){//λ§€κ°œλ³€μˆ˜κ°€ ν•œ 개인 μƒμ„±μž
		this.rc = rc;
		rc.turnOn();
		rc.setVolume(5);
	}
	
	//λ©”μ„œλ“œ
	void methodA() {
		//둜컬 λ³€μˆ˜
	RemoteControl rc = new Audio();
	rc.turnOn();
	rc.setVolume(5);
	}
	
	void methodB(RemoteControl rc) {
	rc.turnOn();
	rc.setVolume(5);
	}
}

예제 2) μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš© MyClassExample.java

public class MyClassExample {
	public static void main(String[] args) {
		System.out.println("1)------------------");
		
		MyClass myClass1 = new MyClass(); //MyClass의 κΈ°λ³Έμƒμ„±μžλ₯Ό 호좜
		myClass1.rc.turnOn();
		myClass1.rc.setVolume(5);
		
		System.out.println("2)------------------");
		MyClass myClass2 = new MyClass(new Audio());//MyClass의 λ§€κ°œλ³€μˆ˜κ°€ ν•œ 개인 μƒμ„±μž 호좜
	
		System.out.println("3)------------------");
		MyClass myClass3 = new MyClass(); //MyClass의 κΈ°λ³Έμƒμ„±μžλ₯Ό 호좜(Television 클래슀)
		myClass3.methodA();
		
		System.out.println("4)------------------");
		MyClass myClass4 = new MyClass();
		myClass4.methodB(new Television());
	}
}

κ²°κ³Ό)

1)------------------
TVλ₯Ό μΌ­λ‹ˆλ‹€.
ν˜„μž¬ TV λ³Όλ₯¨: 5
2)------------------
Audioλ₯Ό μΌ­λ‹ˆλ‹€.
ν˜„μž¬  Audio λ³Όλ₯¨: 5
3)------------------
Audioλ₯Ό μΌ­λ‹ˆλ‹€.
ν˜„μž¬  Audio λ³Όλ₯¨: 5
4)------------------
TVλ₯Ό μΌ­λ‹ˆλ‹€.
ν˜„μž¬ TV λ³Όλ₯¨: 5

마무리 p. 384
5가지 ν‚€μ›Œλ“œλ‘œ λλ‚΄λŠ” 핡심 포인트

1) μΈν„°νŽ˜μ΄μŠ€ (interface) [8μž₯ 370μͺ½]: κ°μ²΄μ˜ μ‚¬μš© 방법을 μ •μ˜ν•œ νƒ€μž…μ΄λ©° 개발 μ½”λ“œμ™€ 객체가 μ„œλ‘œ ν†΅μ‹ ν•˜λŠ” 접점 역할을 ν•œλ‹€. 개발 μ½”λ“œκ°€ μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€λŠ” 객체의 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœμ‹œν‚¨λ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— 개발 μ½”λ“œλŠ” 객체의 λ‚΄λΆ€ ꡬ쑰λ₯Ό μ•Œ ν•„μš”κ°€ μ—†κ³  μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλ§Œ μ•Œκ³  있으면 λœλ‹€.
ꡬ성 λ©€λ²„λŠ” μƒμˆ˜ ν•„λ“œμ™€ 좔상 λ©”μ„œλ“œμ΄λ‹€.

2) μƒμˆ˜ ν•„λ“œ: 
μΈν„°νŽ˜μ΄μŠ€μ˜ μƒμˆ˜ ν•„λ“œλŠ” 기본적으둜 public static final νŠΉμ„±μ„ 가진닀. κ΄€λ‘€μ μœΌλ‘œ ν•„λ“œ 이름은 λͺ¨λ‘ λŒ€λ¬Έμžλ‘œ μž‘μ„±ν•΄μ•Ό ν•˜λ©°, μ„ μ–Έ μ‹œ μ΄ˆκΈ°κ°’μ„ λŒ€μž…ν•΄μ•Ό ν•œλ‹€.

3) 좔상 λ©”μ„œλ“œ: 
μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ λ©”μ„œλ“œλŠ” public abstractκ°€ μƒλž΅λ˜κ³  λ©”μ„œλ“œ μ„ μ–ΈλΆ€λ§Œ μžˆλŠ” 좔상 λ©”μ„œλ“œμ΄λ‹€. κ΅¬ν˜„ ν΄λž˜μŠ€λŠ” λ°˜λ“œμ‹œ 좔상 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•΄μ•Όν•œλ‹€. 

4) implements: 
κ΅¬ν˜„ ν΄λž˜μŠ€λŠ” μ–΄λ–€ μΈν„°νŽ˜μ΄μŠ€λ‘œ μ‚¬μš© κ°€λŠ₯ν•œμ§€(μ–΄λ–€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜κ³  μžˆλŠ”μ§€)λ₯Ό κΈ°μˆ ν•˜κΈ° μœ„ν•΄ 클래슀 μ„ μ–Έ μ‹œ implements ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

5) μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš©: 
클래슀λ₯Ό μ„ μ–Έν•  λ•Œ μΈν„°νŽ˜μ΄μŠ€λŠ” ν•„λ“œ, μƒμ„±μž λ˜λŠ” λ©”μ„œλ“œμ˜ 맀개 λ³€μˆ˜, μƒμ„±μž λ˜λŠ” λ©”μ„œλ“œμ˜ 둜컬 λ³€μˆ˜λ‘œ 선언될 수 μžˆλ‹€.

μΈν„°νŽ˜μ΄μŠ€κ°€ ν•„λ“œ νƒ€μž…μœΌλ‘œ μ‚¬μš©λ  경우:  ν•„λ“œμ— κ΅¬ν˜„ 객체λ₯Ό λŒ€μž…ν•  수 μžˆλ‹€.
μΈν„°νŽ˜μ΄μŠ€κ°€ μƒμ„±μžμ˜ 맀개 λ³€μˆ˜ νƒ€μž…μœΌλ‘œ μ‚¬μš©λ  경우: new μ—°μ‚°μžλ‘œ 객체λ₯Ό 생성할 λ•Œ κ΅¬ν˜„ 객체λ₯Ό μƒμ„±μžμ˜ λ§€κ°œκ°’μœΌλ‘œ λŒ€μž…ν•  수 μžˆλ‹€.
μΈν„°νŽ˜μ΄μŠ€κ°€ 둜컬 λ³€μˆ˜ νƒ€μž…μœΌλ‘œ μ‚¬μš©λ  경우:  λ³€μˆ˜μ— κ΅¬ν˜„ 객체λ₯Ό λŒ€μž…ν•  수 μžˆλ‹€.
μΈν„°νŽ˜μ΄μŠ€κ°€ λ©”μ„œλ“œμ˜ 맀개 λ³€μˆ˜ νƒ€μž…μœΌλ‘œ μ‚¬μš©λ  경우: λ©”μ„œλ“œ 호좜 μ‹œ κ΅¬ν˜„ 객체λ₯Ό λ§€κ°œκ°’μœΌλ‘œ λŒ€μž…ν•  수 μžˆλ‹€.

Q3) main() λ©”μ„œλ“œμ—μ„œ printSound()λ₯Ό ν˜ΈμΆœν•  λ•Œ Catκ³Ό Dog 객체λ₯Ό μ£Όκ³  μ‹€ν–‰ν•˜λ©΄ 각각 "μ•Όμ˜Ή"κ³Ό "멍멍"이 좜λ ₯λ˜λ„λ‘ Catκ³Ό Dog 클래슀λ₯Ό μž‘μ„±ν•˜λΌ.

전체 μ½”λ“œ

예제 1) Soundable μΈν„°νŽ˜μ΄μŠ€
sound()
좔상 λ©”μ„œλ“œλŠ” 객체의 μ†Œλ¦¬λ₯Ό λ¦¬ν„΄ν•œλ‹€. 

package ch08;
public interface Soundable {
	String sound();
}


예제 2) μ•„λž˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

package ch08;
public class Cat implements Soundable {
	//λ©”μ„œλ“œ μž¬μ •μ˜(μ˜€λ²„λΌμ΄λ”©)
	public String sound() {
		return "μ•Όμ˜Ή";
	}
}


예제 3) μ•„λž˜λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

package ch08;
public class Dog implements Soundable {
	//λ©”μ„œλ“œ μž¬μ •μ˜(μ˜€λ²„λΌμ΄λ”©)
	public String sound() {
		return "멍멍";
	}
}


예제 4) SoundableExample ν΄λž˜μŠ€μ—μ„œ printSound() λ©”μ„œλ“œλŠ” Soundable μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μ˜ 맀개 λ³€μˆ˜λ₯Ό 가지고 μžˆλ‹€. 
Soundable soundable = new Cat();

package ch08;
public class SoundableExample {
	public static void printSound(Soundable soundable) {//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…, λ³€μˆ˜
		System.out.println(soundable.sound());
	}
	
	public static void main(String[] args) {
		printSound(new Cat());
		printSound(new Dog());
	}
}

κ²°κ³Ό)

μ•Όμ˜Ή
멍멍

8.2 νƒ€μž… λ³€ν™˜κ³Ό λ‹€ν˜•μ„± p. 386

λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ©”μ„œλ“œ μž¬μ •μ˜μ™€ νƒ€μž… λ³€ν™˜μ΄ ν•„μš”ν•˜λ‹€. μΈν„°νŽ˜μ΄μŠ€λ„ μ œκ³΅ν•˜λŠ” 두 가지 κΈ°λŠ₯으둜 λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•˜λŠ” 데 많이 μ‚¬μš©λœλ‹€. 
상속은 같은 μ’…λ₯˜μ˜ ν•˜μœ„ 클래슀λ₯Ό λ§Œλ“œλŠ” 기술이고, μΈν„°νŽ˜μ΄μŠ€λŠ” μ‚¬μš© 방법이 λ™μΌν•œ 클래슀λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄λ‹€.


8.2.1 μžλ™ νƒ€μž… λ³€ν™˜

μžλ™ νƒ€μž… λ³€ν™˜: κ΅¬ν˜„ κ°μ²΄κ°€ μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜μ— λŒ€μž…λ˜λŠ” κ²ƒ

class Tire{}
          μžμ‹                           λΆ€λͺ¨
class HankookTire extends Tire{}
class KumhoTire extends Tire{}

main(){

Tire t = HankookTire //μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜ (λΆ€λͺ¨(큰) ← μžμ‹(μž‘μ€))

}

Interface Tire{}
         κ΅¬ν˜„ν΄λž˜μŠ€                       μΈν„°νŽ˜μ΄μŠ€
class HankookTire implements Tire{}

main(){
μΈν„°νŽ˜μ΄μŠ€ = κ΅¬ν˜„ν΄λž˜μŠ€
Tire t = HankookTire //μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜(μΈν„°νŽ˜μ΄μŠ€ ← κ΅¬ν˜„)
ν΄λž˜μŠ€μ™€ 클래슀 κ°„μ˜ 관계
λΆ€λͺ¨ 클래슀 νƒ€μž… λ³€μˆ˜ = μžμ‹ 클래슀;

μΈν„°νŽ˜μ΄μŠ€μ™€ 클래슀 κ°„μ˜ 관계
μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜ = κ΅¬ν˜„κ°μ²΄; 

8.2.2 ν•„λ“œμ˜ λ‹€ν˜•μ„± p. 387

λ‹€ν˜•μ„±: μƒμ†μ€ κ°™μ€ μ’…λ₯˜μ˜ ν•˜μœ„ ν΄λž˜μŠ€λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄κ³ , μΈν„°νŽ˜μ΄μŠ€λŠ” μ‚¬μš© λ°©λ²•μ΄ λ™μΌν•œ ν΄λž˜μŠ€λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄λΌλŠ” κ°œλ…μƒμ˜ μ°¨μ΄λŠ” μžˆμ§€λ§Œ λ‘˜ λ‹€ λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•˜λŠ” λ°©λ²•μ€ λΉ„μŠ·ν•˜λ‹€. λͺ¨λ‘ μž¬μ •μ˜μ™€ νƒ€μž… λ³€ν™˜ κΈ°λŠ₯을 μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

타이어: μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… (클래슀 νƒ€μž… X) 
ν•œκ΅­ 타이어, 금호 타이어: κ΅¬ν˜„ 클래슀 (μžμ‹ 클래슀 X)

ν•„λ“œμ˜ λ‹€ν˜•μ„±: Car의 run() λ©”μ„œλ“œλ₯Ό μˆ˜μ •ν•˜μ§€ μ•Šμ•„λ„ λ‹€μ–‘ν•œ roll() λ©”μ„œλ“œμ˜ μ‹€ν–‰κ²°κ³Όλ₯Ό 얻을 수 μžˆλ‹€.

전체 μ½”λ“œ

예제 1) μΈν„°νŽ˜μ΄μŠ€

package ch8_2;
//[1]
public interface Tire {
// public abstract void roll(); (좔상 λ©”μ„œλ“œ)
	public void roll();//rollλ©”μ„œλ“œ 호좜 μ‚¬μš© 방법 μ„€λͺ…
}

예제 2) κ΅¬ν˜„ 클래슀

package ch8_2;
//[2]
public class HankookTire implements Tire{
	@Override//λ©”μ„œλ“œ μž¬μ •μ˜(μ˜€λ²„λΌμ΄λ”©)
	public void roll() { //Tire μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ 
	System.out.println("ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.");
	}	
}

예제 3) κ΅¬ν˜„ 클래슀

package ch8_2;
//[3]
public class KumhoTire implements Tire{
	@Override//λ©”μ„œλ“œ μž¬μ •μ˜(μ˜€λ²„λΌμ΄λ”©)
	public void roll() { //Tire μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ 
	System.out.println("금호 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.");
	}	
}

예제 4) ν•„λ“œ λ‹€ν˜•μ„±

package ch8_2;
//[4]
public class Car {
	//ν•„λ“œ: μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… ν•„λ“œ μ„ μ–Έκ³Ό 초기 κ΅¬ν˜„ 객체 λŒ€μž…
	Tire frontLeftTire = new HankookTire();
	Tire frontRightTire = new HankookTire();
	Tire backLeftTire = new HankookTire();
	Tire backRightTire = new HankookTire();
	//μƒμ„±μž(λ”°λ‘œ μ„ μ–ΈX) - κΈ°λ³Έ μƒμ„±μžκ°€ μžλ™ μΆ”κ°€
	
	//λ©”μ„œλ“œ: μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μ„€λͺ…λœ rol() λ©”μ„œλ“œ 호좜
	void run() {
		frontLeftTire.roll();
		frontRightTire.roll();
		backLeftTire.roll();
		backRightTire.roll();
	}
}

예제 5) ν•„λ“œ λ‹€ν˜•μ„± ν…ŒμŠ€νŠΈ

package ch8_2;
//[5]
public class CarExample {
	public static void main(String[] args) {
		Car myCar = new Car();
		//Car 클래슀의 ν•„λ“œ λ‹€ν˜•μ„±μ΄ HankookTire둜만 λ˜μ–΄ μžˆμ–΄ ν•œκ΅­νƒ€μ΄μ–΄ 4κ°œκ°€ 좜λ ₯
		myCar.run();
		//Car 클래슀의 frontLeftTire, frontRightTireλŠ” κΈˆν˜Ένƒ€μ΄μ–΄λ‘œ ꡐ체
		myCar.frontLeftTire = new KumhoTire();
		myCar.frontRightTire = new KumhoTire();
		
		myCar.run();
	}
}

κ²°κ³Ό) 

ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
금호 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
금호 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.
ν•œκ΅­ 타이어가 κ΅΄λŸ¬κ°‘λ‹ˆλ‹€.

8.2.3 맀개 λ³€μˆ˜μ˜ λ‹€ν˜•μ„± 

λ‹€ν˜•μ„±: μƒμ†μ€ κ°™μ€ μ’…λ₯˜μ˜ ν•˜μœ„ ν΄λž˜μŠ€λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄κ³ , μΈν„°νŽ˜μ΄μŠ€λŠ” μ‚¬μš© λ°©λ²•μ΄ λ™μΌν•œ ν΄λž˜μŠ€λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄λΌλŠ” κ°œλ…μƒμ˜ μ°¨μ΄λŠ” μžˆμ§€λ§Œ λ‘˜ λ‹€ λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•˜λŠ” λ°©λ²•μ€ λΉ„μŠ·ν•˜λ‹€. λͺ¨λ‘ μž¬μ •μ˜μ™€ νƒ€μž… λ³€ν™˜ κΈ°λŠ₯을 μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

예제 1) 맀개 λ³€μˆ˜μ˜ μΈν„°νŽ˜μ΄μŠ€ν™”
Vehicle μ°Έμ‘° λ³€μˆ˜λŠ” vehicle에 μžˆλŠ” μΈν„°νŽ˜μ΄μŠ€μ— μžˆλŠ” κ²ƒλ§Œ μ‹€ν–‰ κ°€λŠ₯ν•˜λ‹€. 

package ch8_2;
//[1]
public class Driver {
	public void drive(Vehicle vehicle) {
//Vehicle vehicle = new Bus();
//	vehicle.run();
//Vehicle vehicle = new Taxi();
		vehicle.run();
	}
}


예제 2) μΈν„°νŽ˜μ΄μŠ€

package ch8_2;
//[2]
public interface Vehicle {
	public void run();
}


예제 3) κ΅¬ν˜„ 클래슀

package ch8_2;
//[3]
public class Bus implements Vehicle{
	@Override //λ©”μ„œλ“œ μž¬μ •μ˜
	public void run() {
		System.out.println("λ²„μŠ€κ°€ λ‹¬λ¦½λ‹ˆλ‹€.");
	}
}

예제 4) κ΅¬ν˜„ 클래슀

package ch8_2;
//[4]
public class Taxi implements Vehicle{
	@Override //λ©”μ„œλ“œ μž¬μ •μ˜
	public void run() {
		System.out.println("νƒμ‹œκ°€ λ‹¬λ¦½λ‹ˆλ‹€.");
	}
}


예제 5) 맀개 λ³€μˆ˜μ˜ λ‹€ν˜•μ„± ν…ŒμŠ€νŠΈ

package ch8_2;
//[5]
public class DriverExample {
	public static void main(String[] args) {
		Driver driver = new Driver();
		
		Bus bus = new Bus();
		Taxi taxi = new Taxi();
		
		driver.drive(bus);//μžλ™νƒ€μž…λ³€ν™˜:Vehicle vehicle=bus;
		driver.drive(taxi);//μžλ™νƒ€μž…λ³€ν™˜:Vehicle vehicle=taxi;
	}
}

κ²°κ³Ό)

λ²„μŠ€κ°€ λ‹¬λ¦½λ‹ˆλ‹€.
νƒμ‹œκ°€ λ‹¬λ¦½λ‹ˆλ‹€.

8.2.4 κ°•μ œ νƒ€μž… λ³€ν™˜ (Casting) p. 394

κ΅¬ν˜„ 객체가 μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μœΌλ‘œ μžλ™ νƒ€μž… λ³€ν™˜ν•˜λ©΄, μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έλœ λ©”μ„œλ“œλ§Œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€λŠ” μ œμ•½ 사항이 λ”°λ₯Έλ‹€. ex) μΈν„°νŽ˜μ΄μŠ€μ— 3개의 λ©”μ„œλ“œκ°€ μ„ μ–Έλ˜μ–΄ 있고 ν΄λž˜μŠ€μ—λŠ” 5개의 λ©”μ„œλ“œκ°€ μ„ μ–Έλ˜μ–΄ μžˆλ‹€λ©΄, μΈν„°νŽ˜μ΄μŠ€λ‘œ 호좜 κ°€λŠ₯ν•œ λ©”μ„œλ“œλŠ” 3개 뿐이닀.

κ΅¬ν˜„ ν΄λž˜μŠ€μ— μ„ μ–Έλœ ν•„λ“œμ™€ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•  κ²½μš°λ„ λ°œμƒν•œλ‹€. μ΄λ•Œ κ°•μ œ νƒ€μž… λ³€ν™˜μ„ ν•΄μ„œ λ‹€μ‹œ κ΅¬ν˜„ 클래슀 νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•œ λ‹€μŒ, κ΅¬ν˜„ 클래슀의 ν•„λ“œμ™€ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

κ°•μ œ νƒ€μž… λ³€ν™˜: μΈν„°νŽ˜μ΄μŠ€μ— λŒ€μž…λœ κ΅¬ν˜„ κ°μ²΄λ₯Ό λ‹€μ‹œ μ›λž˜ κ΅¬ν˜„ ν΄λž˜μŠ€ νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•˜λŠ” κ²ƒ

κ΅¬ν˜„ν΄λž˜μŠ€ λ³€μˆ˜ = (κ΅¬ν˜„ν΄λž˜μŠ€) μΈν„°νŽ˜μ΄μŠ€λ³€μˆ˜;
int a = 65;
//int <- char
//4byte <- 2byte
int b = 'A' (μžλ™ νƒ€μž… λ³€ν™˜) 

char c = (char)a; 

예제 1) μΈν„°νŽ˜μ΄μŠ€

public interface Vehicle {
	public void run();
}

예제 2) κ΅¬ν˜„ 클래슀

public class Bus implements Vehicle {

	@Override
	public void run() {
		System.out.println("λ²„μŠ€κ°€ λ‹¬λ¦½λ‹ˆλ‹€.");
	}
	
	public void checkFare() {
		System.out.println("μŠΉμ°¨μš”κΈˆμ„ μ²΄ν¬ν•©λ‹ˆλ‹€.");
	}
}

예제 3) κ°•μ œ νƒ€μž… λ³€ν™˜

public class VehicleExample {
	public static void main(String[] args) {
		//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜)
		Vehicle vehicle = new Bus();
		
		vehicle.run();
		//vehicle.checkFare(); (X): Vehicle μΈν„°νŽ˜μ΄μŠ€μ—λŠ” checkFare()κ°€ μ—†μŒ
		
		//Busν΄λž˜μŠ€μ— checkFareλ©”μ„œλ“œκ°€ 있기 λ•Œλ¬Έμ— 호좜 κ°€λŠ₯
		//κ΅¬ν˜„κ°μ²΄νƒ€μž… = μΈν„°νŽ˜μ΄μŠ€(μ°Έμ‘° κ°•μ œ νƒ€μž… λ³€ν™˜)
		Bus bus = (Bus) vehicle; //κ°•μ œ νƒ€μž… λ³€ν™˜
		
		bus.run();
		bus.checkFare(); //Bus ν΄λž˜μŠ€μ—λŠ” CheckFare()κ°€ 있음
	}
}

8.2.5 객체 νƒ€μž… 확인 

μš°λ¦¬λŠ” μƒμ†μ—μ„œ 객체 νƒ€μž…μ„ ν™•μΈν•˜κΈ° μœ„ν•΄ instanceof μ—°μ‚°μžλ₯Ό μ‚¬μš©ν–ˆκ³ , μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆλ‹€.

instanceof: κ°•μ œ νƒ€μž… λ³€ν™˜μ„ ν•˜κΈ° 전에 λ³€ν™˜μ΄ κ°€λŠ₯ν•œμ§€ 쑰사할 λ•Œ μ‚¬μš©ν•œλ‹€. μƒμ†μ—μ„œλŠ” μžμ‹ 클래슀 νƒ€μž…μΈμ§€, μΈν„°νŽ˜μ΄μŠ€μ—μ„œλŠ” κ΅¬ν˜„ 클래슀 νƒ€μž…μΈμ§€λ₯Ό 확인할 λ•Œ μ‚¬μš©ν•œλ‹€.

ex) Vehicle μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μœΌλ‘œ λ³€ν™˜λœ 객체가 Bus인지 ν™•μΈν•˜λ €λ©΄ μ•„λž˜μ²˜λŸΌ μž‘μ„±ν•œλ‹€.

if(vehicle instanceof Bus){
	Bus bus = (Bus) vehicle;
}

예제 1) 객체 νƒ€μž… 확인

public class Driver{
	public void drive(Vehicle vehicle) {//vehicle: Bus, Taxi 객체
		if(vehicle instanceof Bus) {//vehicle 맀개 λ³€μˆ˜κ°€ μ°Έμ‘°ν•˜λŠ” 객체가 Bus인지 쑰사
			Bus bus = (Bus) vehicle;//Bus 객체일 경우 μ•ˆμ „ν•˜κ²Œ κ°•μ œ νƒ€μž… λ³€ν™˜
			bus.checkFare();//Bus νƒ€μž…μœΌλ‘œ κ°•μ œ νƒ€μž… λ³€ν™˜μ„ ν•˜λŠ” 이유
		}
		vehicle.run();
	}
}


예제 2) 객체 νƒ€μž… 확인

public class DriverExample{
	public static void main(String[] args) {
		Driver driver = new Driver();
		
		Bus bus = new Bus();
		Taxi taxi = new Taxi();
		
		driver.drive(bus);
		driver.drive(taxi);
	}
}

μ’€ 더 μ•Œμ•„λ³΄κΈ°) μΈν„°νŽ˜μ΄μŠ€ 상속 p. 399

μΈν„°νŽ˜μ΄μŠ€ 상속: ν΄λž˜μŠ€λŠ” 닀쀑 상속을 ν—ˆμš©ν•˜μ§€ μ•Šμ§€λ§Œ, μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑 상속을 ν—ˆμš©ν•œλ‹€. 즉 extends ν‚€μ›Œλ“œ 뒀에 μƒμœ„ μΈν„°νŽ˜μ΄μŠ€κ°€ 올 수 μžˆλ‹€.

예제) μƒμœ„ μΈν„°νŽ˜μ΄μŠ€

package ch8_2;
//[1]
public interface InterfaceA {
	//public void abstract methodA();
	public void methodA();
}


예제) μƒμœ„ μΈν„°νŽ˜μ΄μŠ€

package ch8_2;
//[2]
public interface InterfaceB {
	//public void abstract InterfaceB();
	public void methodB();
}


예제) ν•˜μœ„ μΈν„°νŽ˜μ΄μŠ€

package ch8_2;
//[3]
public interface InterfaceC extends InterfaceA, InterfaceB {
	//public void methodA();
	//public void methodB();
	//public void abstract methodC();
	public void methodC();
}


예제) ν•˜μœ„ μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

package ch8_2;
//[4]
public class ImplementationC implements InterfaceC{
	public void methodA() {
		System.out.println("ImplementationC-methodA() μ‹€ν–‰");
	}
	
	public void methodB() {
		System.out.println("ImplementationC-methodB() μ‹€ν–‰");
	}
	
	public void methodC() {
		System.out.println("ImplementationC-methodC() μ‹€ν–‰");
	}
}


예제) 호좜 κΈ°λŠ₯ λ©”μ„œλ“œ

package ch8_2;
//[5]
public class Example {
	public static void main(String[] args) {
		ImplementationC impl = new ImplementationC();
		//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜)
		InterfaceA ia = impl;
		ia.methodA();
		System.out.println();
		
		//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜)
		InterfaceB ib = impl;
		ib.methodB();
		System.out.println();
		
		//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μ°Έμ‘° μžλ™ νƒ€μž… λ³€ν™˜)
		InterfaceC ic = impl;
		ic.methodA();
		ic.methodB();
		ic.methodC();
	}
}


κ²°κ³Ό)

ImplementationC-methodA() μ‹€ν–‰

ImplementationC-methodB() μ‹€ν–‰

ImplementationC-methodA() μ‹€ν–‰
ImplementationC-methodB() μ‹€ν–‰
ImplementationC-methodC() μ‹€ν–‰

마무리
5가지 ν‚€μ›Œλ“œλ‘œ λλ‚΄λŠ” 핡심 포인트

1) μžλ™ νƒ€μž… λ³€ν™˜: κ΅¬ν˜„ 객체가 μΈν„°νŽ˜μ΄μŠ€ λ³€μˆ˜μ— λŒ€μž…λ˜λŠ” 것

2) λ‹€ν˜•μ„±: μƒμ†μ€ 같은 μ’…λ₯˜μ˜ ν•˜μœ„ 클래슀λ₯Ό λ§Œλ“œλŠ” 기술이고, μΈν„°νŽ˜μ΄μŠ€λŠ” μ‚¬μš© 방법이 λ™μΌν•œ 클래슀λ₯Ό λ§Œλ“œλŠ” κΈ°μˆ μ΄λΌλŠ” κ°œλ…μƒμ˜ μ°¨μ΄λŠ” μžˆμ§€λ§Œ λ‘˜ λ‹€ λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•˜λŠ” 방법은 λΉ„μŠ·ν•˜λ‹€. λͺ¨λ‘ μž¬μ •μ˜μ™€ νƒ€μž… λ³€ν™˜ κΈ°λŠ₯을 μ œκ³΅ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

3) κ°•μ œ νƒ€μž… λ³€ν™˜: μΈν„°νŽ˜μ΄μŠ€μ— λŒ€μž…λœ κ΅¬ν˜„ 객체λ₯Ό λ‹€μ‹œ μ›λž˜ κ΅¬ν˜„ 클래슀 νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•˜λŠ” 것

4) instanceof: κ°•μ œ νƒ€μž… λ³€ν™˜μ„ ν•˜κΈ° 전에 λ³€ν™˜μ΄ κ°€λŠ₯ν•œμ§€ 쑰사할 λ•Œ μ‚¬μš©ν•œλ‹€. μƒμ†μ—μ„œλŠ” μžμ‹ 클래슀 νƒ€μž…μΈμ§€, μΈν„°νŽ˜μ΄μŠ€μ—μ„œλŠ” κ΅¬ν˜„ 클래슀 νƒ€μž…μΈμ§€λ₯Ό 확인할 λ•Œ μ‚¬μš©ν•œλ‹€.

5) μΈν„°νŽ˜μ΄μŠ€ 상속: ν΄λž˜μŠ€λŠ” 닀쀑 상속을 ν—ˆμš©ν•˜μ§€ μ•Šμ§€λ§Œ, μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑 상속을 ν—ˆμš©ν•œλ‹€. 즉 extends ν‚€μ›Œλ“œ 뒀에 μƒμœ„ μΈν„°νŽ˜μ΄μŠ€κ°€ 올 수 μžˆλ‹€.

class A{}
class B extends A{}

main(){
          // μƒμ†κ΄€κ³„μ˜ μžλ™ νƒ€μž… λ³€ν™˜
          // λΆ€λͺ¨ν΄λž˜μŠ€ νƒ€μž…(A) μžμ‹ν΄λž˜μŠ€(B)         
          A a = new B();          

          // μƒμ†κ΄€κ³„μ˜ κ°•μ œ νƒ€μž… λ³€ν™˜          
          // μžμ‹ν΄λž˜μŠ€ νƒ€μž…(B) λΆ€λͺ¨ν΄λž˜μŠ€(A)         
          B() = (B) a;
}
======================================
interface A{} // μΈν„°νŽ˜μ΄μŠ€

class B implements A{} // κ΅¬ν˜„κ°μ²΄

main(){
          // μΈν„°νŽ˜μ΄μŠ€μ™€ κ΅¬ν˜„ 객체 κ΄€κ³„μ˜ μžλ™ νƒ€μž… λ³€ν™˜
          // μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…(A) μžμ‹ 클래슀 (B)
          A a = new B();         

          // μΈν„°νŽ˜μ΄μŠ€μ™€ κ΅¬ν˜„ 객체 κ΄€κ³„μ˜ κ°•μ œ νƒ€μž… λ³€ν™˜         
          // κ΅¬ν˜„κ°μ²΄ νƒ€μž…(B) ← μΈν„°νŽ˜μ΄μŠ€(A)
          B b = (B) a;
}

μ°Έκ³ : http://alecture.blogspot.com/2011/05/abstract-class-interface.html


확인 문제
Q1 (4) λ©”μ„œλ“œμ˜ λ§€κ°œλ³€μˆ˜ νƒ€μž…μ΄ μΈν„°νŽ˜μ΄μŠ€μ΄λ©΄ λ§€κ°œκ°’μœΌλ‘œ λͺ¨λ“  κ΅¬ν˜„ 객체λ₯Ό λŒ€μž…ν•˜λ©΄ μžλ™ νƒ€μž… λ³€ν™˜μ΄ λœλ‹€. (O)

interface A{
          // 좔상 λ©”μ„œλ“œ
          void add(A a); //μΈν„°νŽ˜μ΄μŠ€
}

class B implements A{
          // κ΅¬ν˜„ λ©”μ„œλ“œ
          add(B) //κ΅¬ν˜„ 객체 λ³€μˆ˜ 
}
μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μžλ™ νƒ€μž… λ³€ν™˜)
          A a            = new(B);

Q3 DaoExample 클래슀의 main() λ©”μ„œλ“œμ—μ„œ dbWork() λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ OracleDao와 MysqlDao 객체λ₯Ό λ§€κ°œκ°’μœΌλ‘œ μ£Όκ³  ν˜ΈμΆœν–ˆλ‹€. 
dbWork() λ©”μ„œλ“œλŠ” 두 객체λ₯Ό λͺ¨λ‘ λ§€κ°œκ°’μœΌλ‘œ λ°›κΈ° μœ„ν•΄ DataAccessObject νƒ€μž…μ˜ 맀개 λ³€μˆ˜λ₯Ό 가지고 μžˆλ‹€.
μ‹€ν–‰ κ²°κ³Όλ₯Ό 보고 DataAccessObject μΈν„°νŽ˜μ΄μŠ€μ™€ OracleDao, MysqlDao κ΅¬ν˜„ 클래슀λ₯Ό 각각 μž‘μ„±ν•˜λΌ.

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

OracleDBμ—μ„œ 검색
OracleDB에 μ‚½μž…
OracleDBλ₯Ό μˆ˜μ •
OracleDBλ₯Ό μ‚­μ œ
Mysqlμ—μ„œ 검색
Mysql에 μ‚½μž…
Mysqlλ₯Ό μˆ˜μ •
Mysqlλ₯Ό μ‚­μ œ
μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… = κ΅¬ν˜„ 객체 (μžλ™ νƒ€μž… λ³€ν™˜)
DataAccessObject dao = new OracleDao();

전체 μ½”λ“œ

예제 1) DaoExample.java
dbWork() λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ OracleDao와 MysqlDao 객체λ₯Ό λ§€κ°œκ°’μœΌλ‘œ μ£Όκ³  ν˜ΈμΆœν•œλ‹€.

package ch08;
//[1]
public class DaoExample {
								//μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…
	public static void dbWork(DataAccessObject dao) {
		dao.select();
		dao.insert();
		dao.update();
		dao.delete();
	}//dbWork 끝

	public static void main(String[] args) {
		//κ΅¬ν˜„κ°μ²΄
		dbWork(new OracleDao());
		//κ΅¬ν˜„κ°μ²΄
		dbWork(new MysqlDao());
	}
}

예제 2) DataAccessObject.java μΈν„°νŽ˜μ΄μŠ€

package ch08;
//[2]
public interface DataAccessObject {
	//좔상 λ©”μ„œλ“œ
	public void select();
	public void insert();
	public void update();
	public void delete();
}

예제 3) MysqlDao.java κ΅¬ν˜„ 클래슀

package ch08;
//[3]
public class MysqlDao implements DataAccessObject{

		public void select() {
			System.out.println("Mysqlμ—μ„œ 검색");
		}
		public void insert() {
			System.out.println("Mysql에 μ‚½μž…");
		}
		public void update() {
			System.out.println("Mysqlλ₯Ό μˆ˜μ •");
		}
		public void delete() {
			System.out.println("Mysqlλ₯Ό μ‚­μ œ");
		}		
}

예제 4) OracleDao.java κ΅¬ν˜„ 클래슀

package ch08;
//[4]
public class OracleDao implements DataAccessObject{
	//DataAccessObject interface의 좔상 λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜λŠ” κ΅¬ν˜„λ©”μ„œλ“œ
	public void select() {
		System.out.println("OracleDBμ—μ„œ 검색");
	}
	public void insert() {
		System.out.println("OracleDB에 μ‚½μž…");
	}
	public void update() {
		System.out.println("OracleDBλ₯Ό μˆ˜μ •");
	}
	public void delete() {
		System.out.println("OracleDBλ₯Ό μ‚­μ œ");
	}
}

728x90
λ°˜μ‘ν˜•
Comments