Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[35] 230216 Java Ch. 7 ์ƒ์†: 2. ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋‹คํ˜•์„ฑ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 35์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[35] 230216 Java Ch. 7 ์ƒ์†: 2. ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋‹คํ˜•์„ฑ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 35์ผ]

yjyuwisely 2023. 2. 16. 12:50

230216 Thu 35th class

Ch. 7 ์ƒ์†
์ง„๋„: p. 332 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)
ํ•œ๋น›๋ฏธ๋””์–ด_ํ˜ผ๊ณต์ž_์šฉ์–ด์ง‘.pdf
์šฉ์–ด ์ฐธ๊ณ 

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ์‹ ์šฉ๊ถŒ - ๊ต๋ณด๋ฌธ๊ณ 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ํ˜ผ์ž ํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค! 1:1 ๊ณผ์™ธํ•˜๋“ฏ ๋ฐฐ์šฐ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž์Šต์„œ (JAVA 8 &11 ์ง€์›) ์ด ์ฑ…์€ ๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค. 


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

2023.01.31 - [1. Revision 2023/Java] - [23] 230131 Ch. 2 ์ž๋ฐ” ๋ณ€์ˆ˜์™€ ํƒ€์ž…(2): ํƒ€์ž… ๋ณ€ํ™˜, ๋ณ€์ˆ˜์™€ ์‹œ์Šคํ…œ ์ž…์ถœ๋ ฅ

  • Widening Casting (automatically) - converting a smaller type to a larger type size
     ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ํฐ ํƒ€์ž… = ์ž‘์€ ํƒ€์ž…(์ž‘์€ ํƒ€์ž… → ํฐ ํƒ€์ž…)

    byte  short  char  int  long  float  double
    Widening casting is done automatically when passing a smaller size type to a larger size type.
    int myInt = 9;
    double myDouble = myInt; // Automatic casting: int to double

    System.out.println(myInt);      // Outputs 9
    System.out.println(myDouble);   // Outputs 9.0
  • Narrowing Casting (manually) - converting a larger type to a smaller size type
    ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜: ์ž‘์€ ํƒ€์ž… = (์ž‘์€ ํƒ€์ž…)ํฐ ํƒ€์ž…(ํฐ ํƒ€์ž… → ์ž‘์€ ํƒ€์ž…)

    double  float  long  int  char  short  byte
    Narrowing casting must be done manually by placing the type in parentheses in front of the value.
    double myDouble = 9.78d;
    int myInt = (int) myDouble; // Manual casting: double to int

    System.out.println(myDouble);   // Outputs 9.78
    System.out.println(myInt);      // Outputs 9

ํƒ€์ž… ๋ณ€ํ™˜
byte → short → char → int → long → float → double

int a = 10; 
long b = a; (์ž‘์€ -> ํฐ: ์ž๋™)

char c = 'A';
int d = c; (์ž‘์€ -> ํฐ: ์ž๋™)

int e = 65;
char f = e; (ํฐ -> ์ž‘์€: ๊ฐ•์ œ)


2023.02.10 - [1. Revision 2023/Java] - [31] 230210 Ch. 6 ํด๋ž˜์Šค(2): ๋ฉ”์„œ๋“œ

void: Used at method declaration and definition to specify that the method does not return any type.

๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ void๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
void๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ์—์„œ๋„ return๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌํ„ด๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฉ”์„œ๋“œ ์‹คํ–‰์„ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ์—ญํ• ์ด๋‹ค. 


๋ฆฌํ„ด๋ฌธ:
๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ๋ฆฌํ„ด ํƒ€์ž…์ด ์žˆ๋‹ค๋ฉด ๋ฆฌํ„ด๊ฐ’์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด return๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ฆฌํ„ด ํƒ€์ž…์ด void๋ผ๋ฉด return๋ฌธ์€ ํ•„์š” ์—†์ง€๋งŒ, ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ข…๋ฃŒ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

return (null): null๋กœ returnํ•œ๋‹ค.
submit์„ ํ•˜์ง€ ๋ชปํ•˜๊ณ  null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ด๋‹น return ์•„๋ž˜๋ฅผ ์ „๋ถ€ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  return ์ค„์—์„œ ํด๋ž˜์Šค๋ฅผ ๋๋‚ด๋ฒ„๋ฆฐ๋‹ค.

return true: submit ํ•˜๊ธฐ ์ „์— ํ•จ์ˆ˜์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  ํ†ต๊ณผ๊ฐ€ ๋˜๋ฉด true(=submit true)๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

return false: ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์œผ๋ฉด submit์„ ํ•˜์ง€ ์•Š๊ณ  fasle๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป(return null๊ณผ ์•„์ฃผ ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.)์ด๋‹ค.

์ฐธ๊ณ : https://diaryofgreen.tistory.com/80


Java์—์„œ sysout + [Ctrl] + [space] = System.out.println(); ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.


 

2023.02.09 - [1. Revision 2023/Java] - [30] 230209 Ch. 6 ํด๋ž˜์Šค(1): ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ํ•„๋“œ, ์ƒ์„ฑ์ž

new๋Š” ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. (์ƒ์„ฑ์ž: ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ)
new ์—ฐ์‚ฐ์ž๋Š” ํž™ ์˜์—ญ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚จ ํ›„ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋˜์–ด ์žˆ๋‹ค. 

//ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์— new ์—ฐ์‚ฐ์ž๊ฐ€ ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜;
๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

//1๊ฐœ์˜ ์‹คํ–‰๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

this, this() ์ฐจ์ด
this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๊ฐ€๋ฅดํ‚ค๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๊ณ  this()๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋œปํ•œ๋‹ค.
์ฐธ๊ณ : https://lowelllll.github.io/java/2018/12/09/java-this-%EC%B0%A8%EC%9D%B4%EC%A0%90/
์ฐธ๊ณ : https://itprogramming119.tistory.com/149


 

Ch. 7 ์ƒ์†

์ฑ…์˜ ๋ชฉ์ฐจ
07-1 ์ƒ์†
07-2 ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋‹คํ˜•์„ฑ
07-3 ์ถ”์ƒ ํด๋ž˜์Šค

7.2 ํƒ€์ž… ๋ณ€ํ™˜(type conversion)๊ณผ ๋‹คํ˜•์„ฑ(polymorphism)

์ƒ์†์ด ์ด๋ฃจ์–ด์ ธ์•ผํ•œ๋‹ค. → ํฌ๋‹ค(๋ถ€๋ชจ), ์ž‘๋‹ค(์ž์‹)์ด ๋œ๋‹ค. 
๋ถ€๋ชจ(ํผ) → ์ž์‹(์ž‘์Œ): ๊ฐ•์ œ
๋ถ€๋ชจ(์ž‘์€) → ์ž์‹(ํผ): ์ž๋™

๋‹คํ˜•์„ฑ(polymorphism): ๊ฐ™์€ ํƒ€์ž…์ด์ง€๋งŒ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค์–‘ํ•œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์„ฑ์งˆ์„ ๋งํ•œ๋‹ค. ์ฝ”๋“œ ์ธก๋ฉด์—์„œ ๋ณด๋ฉด ๋‹คํ˜•์„ฑ์€ ํ•˜๋‚˜์˜ ํƒ€์ž…์— ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋Œ€์ž…ํ•จ์œผ๋กœ์จ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋™์ผํ•˜์ง€๋งŒ ๋‹ค์–‘ํ•œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ๋‹ค์–‘ํ•œ ์‹คํ–‰๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ•˜๋Š” ์„ฑ์งˆ์ด๋‹ค.

class ๋™๋ฌผ{
}

class ๊ณ ์–‘์ด extends ๋™๋ฌผ{
}

class ๊ฐ•์•„์ง€ extends ๋™๋ฌผ{
}

class ๋™๋ฌผํ…Œ์ŠคํŠธ(){
          ๋™๋ฌผ = ๊ณ ์–‘์ด
          ๋™๋ฌผ = ๊ฐ•์•„์ง€
}

1) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜(Promotion)

ํƒ€์ž… ๋ณ€ํ™˜: ํƒ€์ž…์„ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ–‰์œ„

ํด๋ž˜์Šค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํƒ€์ž… ๋ณ€ํ™˜์ด ์žˆ๋Š”๋ฐ, ํด๋ž˜์Šค์˜ ๋ฐ˜ํ™˜์€ ์ƒ์† ๊ด€๊ณ„์— ์žˆ๋Š” ํด๋ž˜์Šค ์‚ฌ์ด์—์„œ ๋ฐœ์ƒํ•œ๋‹ค.
์ž์‹์€ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜: ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๋„์ค‘์— ์ž๋™์ ์œผ๋กœ ํƒ€์ž… ๋ณ€ํ™˜์ด ์ผ์–ด๋‚˜๋Š” ๊ฒƒ
์กฐ๊ฑด:

<- ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ <-
๋ถ€๋ชจํƒ€์ž… ๋ณ€์ˆ˜ = ์ž์‹ ํƒ€์ž…;
Cat cat = new Cat();
Animal animal = cat;
//์•„๋ž˜์™€ ๊ฐ™๋‹ค.
Animal animal = new Cat();

์ž์‹์€ ๋ถ€๋ชจ์˜ ํŠน์ง•๊ณผ ๊ธฐ๋Šฅ์„ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ์™€ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰๋  ์ˆ˜ ์žˆ๋‹ค.

cat == animal //true

์ฐธ์กฐ ๋ณ€์ˆ˜์˜ == ์—ฐ์‚ฐ์€ ์ฐธ์กฐ ๋ฒˆ์ง€๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ true๋ฅผ ์‚ฐ์ถœํ•˜๋ฏ€๋กœ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค.

๋ฐ”๋กœ ์œ„์˜ ๋ถ€๋ชจ๊ฐ€ ์•„๋‹ˆ๋”๋ผ๊ณ  ์ƒ์† ๊ณ„์ธต์—์„œ ์ƒ์œ„ ํƒ€์ž…์ด๋ผ๋ฉด ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ 1) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜

class A {}
//	์ž์‹		<	๋ถ€๋ชจ
class B extends A {}
//์ž์‹		<	๋ถ€๋ชจ
class C extends A {}

//์ž์‹		<	๋ถ€๋ชจ
class D extends B {}
//์ž์‹		<	๋ถ€๋ชจ
class E extends C {}

public class PromotionExample {
	public static void main(String[] args) {
		
		B b = new B();
		C c = new C();
		D d = new D();
		E e = new E();
		
		//int a = 10;
		A a1 = b; //A: ํด๋ž˜์Šค ํƒ€์ž…, ์ฐธ์กฐ ํƒ€์ž…, a1: ์ฐธ์กฐ ๋ณ€์ˆ˜
		A a2 = c;
		A a3 = d;
		A a4 = e;
		
		B b1 = d;
		C c1 = e;
		
		//์ƒ์† ๊ด€๊ณ„์— ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
		//B b3 = e; 
		//C c2 = d;
	}
}

[์ค‘์š”!] ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜๋œ ์ดํ›„์—๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 
๋น„๋ก ๋ณ€์ˆ˜๋Š” ์ž์‹ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€๋งŒ ๋ณ€์ˆ˜๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ฉค๋ฒ„๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค ๋ฉค๋ฒ„๋กœ ํ•œ์ •๋œ๋‹ค.

์˜ˆ์™ธ) ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜(์˜ค๋ฒ„๋ผ์ด๋”ฉ)๋˜์—ˆ๋‹ค๋ฉด ์ž์‹ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋Œ€์‹  ํ˜ธ์ถœ๋œ๋‹ค.

์˜ˆ์ œ 1) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„์˜ ๋ฉค๋ฒ„ ์ ‘๊ทผ Parent.java

public class Parent {
	int a = 10;
	public void method1() {
	System.out.println("Parent-method1()");
	}
	public void method2() {
	System.out.println("Parent-method2()");
	}
}

์˜ˆ์ œ 2) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„์˜ ๋ฉค๋ฒ„ ์ ‘๊ทผ Child.java

public class Child  extends Parent{
	int a = 20;
	public void method2(){
		System.out.println("Child-method2()");
	}
	public void method3(){
		System.out.println("Child-method3()");
	}
}

์˜ˆ์ œ 3) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„์˜ ๋ฉค๋ฒ„ ์ ‘๊ทผ ChildExample.java

public class ChildExample {
	public static void main(String[] args) {
		//int a = 10;
		Child child = new Child();

		Parent parent = child; //์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜
		
		System.out.println(parent.a);
		
		parent.method1();
		parent.method2(); //์žฌ์ •์˜๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋จ
		//parent.method3(); //ํ˜ธ์ถœ ๋ถˆ๊ฐ€๋Šฅ
	}
}

๊ฒฐ๊ณผ)

10
Parent-method1()
Child-method2()

2) ํ•„๋“œ์˜ ๋‹คํ˜•์„ฑ p. 337

๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. 
ํ•„๋“œ์˜ ํƒ€์ž…์„ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์ž์‹ ๊ฐ์ฒด๋“ค์ด ์ €์žฅ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„๋“œ ์‚ฌ์šฉ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. (=ํ•„๋“œ์˜ ๋‹คํ˜•์„ฑ)
ํ”„๋กœ๊ทธ๋žจ์€ ์ˆ˜๋งŽ์€ ๊ฐ์ฒด๋“ค์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜๊ณ  ๊ฐ์ž์˜ ์—ญํ• ์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๊ฐ์ฒด๋“ค์€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ๊ต์ฒด๋  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

ex) ์ž๋™์ฐจ ํด๋ž˜์Šค๋ฅผ ์ฒ˜์Œ ์„ค๊ณ„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ํƒ€์ด์–ด ๊ฐ์ฒด๋Š” ์–ธ์ œ๋“ ์ง€ ์„ฑ๋Šฅ์ด ์ข‹์€ ๋‹ค๋ฅธ ํƒ€์ด์–ด ๊ฐ์ฒด๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 
์ƒˆ๋กœ ๊ต์ฒด๋˜๋Š” ํƒ€์ด์–ด ๊ฐ์ฒด๋Š” ๊ธฐ์กด ํƒ€์ด์–ด์™€ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋™์ผํ•˜์ง€๋งŒ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋” ์šฐ์ˆ˜ํ•˜๊ฒŒ ๋‚˜์™€์•ผ ํ•  ๊ฒƒ์ด๋‹ค. 
→ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์†๊ณผ ์žฌ์ •์˜, ํƒ€์ž… ๋ณ€ํ™˜์„ ์ด์šฉํ•œ๋‹ค. 

[๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์  ์กฐ๊ฑด] 
1. ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋‹ˆ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด ๋™์ผํ•˜๋‹ค.
2. ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์„œ ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•จ์œผ๋กœ์จ ๋” ์šฐ์ˆ˜ํ•œ ์‹คํ–‰๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 
3. ์ž์‹ ํƒ€์ž…์„ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค


๋‹คํ˜•์„ฑ: ์ „์ฒด ์ˆ˜์ •์ด ์•„๋‹Œ, ํŠน์ • ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•œ๋‹ค. (์œ ์ง€๋ณด์ˆ˜), ํšจ์œจ์ ์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค. 
ex) ํƒ€์ด์–ด 4๊ฐœ์˜ ์ž๋™์ฐจ์˜ ๊ฒฝ์šฐ ์•ž ํƒ€์ด์–ด 2๊ฐœ, ๋’ท ํƒ€์ด์–ด 2๊ฐœ ๋‚˜๋ˆ„๋Š” ๊ฒฝ์šฐ 
์ฑ…์˜ ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” ํƒ€์ด์–ด๋ฅผ 4๊ฐœ๋กœ ๋‚˜๋‰˜์—ˆ๋‹ค. 

1) ~ 4)์˜ ์ „์ฒด ์ฝ”๋“œ

1) Tire  ํด๋ž˜์Šค

์ตœ๋Œ€ ํšŒ์ „์ˆ˜ = ํƒ€์ด์–ด์˜ ์ˆ˜๋ช…, ์ตœ๋Œ€ ํšŒ์ „์ˆ˜์— ๋„๋‹ฌํ•˜๋ฉด ํƒ€์ด์–ด์— ํŽ‘ํฌ๊ฐ€ ๋‚œ๋‹ค. 

์˜ˆ์ œ 1) Tire.java
if, else์ผ ๋•Œ ๊ฐ๊ฐ return true; return false; ์“ด๋‹ค.

package ch07_2;
//[1]
public class Tire {
	//ํ•„๋“œ(๊ธฐ๋ณธ ํƒ€์ž…)
	public int maxRotation;//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ public์ธ ์ธ์Šคํ„ด์Šค ํ•„๋“œ: ์ตœ๋Œ€ ํšŒ์ „์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ•„๋“œ(ํƒ€์ด์–ด ์ˆ˜๋ช…) 
	public int accumulatedRotation;//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ public์ธ ์ธ์Šคํ„ด์Šค ํ•„๋“œ: ๋ˆ„์  ํšŒ์ „์ˆ˜
	public String location;//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ public์ธ ์ธ์Šคํ„ด์Šค ํ•„๋“œ: ํƒ€์ด์–ด์˜ ์œ„์น˜(์•ž์™ผ์ชฝ, ์•ž์˜ค๋ฅธ์ชฝ, ๋’ค์™ผ์ชฝ, ๋’ค์˜ค๋ฅธ์ชฝ)
	
	//์ƒ์„ฑ์ž(๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ์ธ ์ƒ์„ฑ์ž: ํƒ€์ด์–ด์œ„์น˜, ์ตœ๋Œ€ ํšŒ์ „์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ƒ์„ฑ์ž)
	public Tire(String location, int maxRotation) {
		
		 //ํƒ€์ด์–ด์˜ ์œ„์น˜ ์ดˆ๊ธฐํ™”
		this.location = location;
		
		 //์ตœ๋Œ€ ํšŒ์ „์ˆ˜ ์ดˆ๊ธฐํ™”
		this.maxRotation = maxRotation;
	}
	
	//๋ฉ”์„œ๋“œ
	public boolean roll() {//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ public์ด๊ณ , ๋ฆฌํ„ดํƒ€์ž…์€ boolean์ธ roll์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ 
		//๋ˆ„์  ํšŒ์ „ ์ˆ˜ 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์„œ ๋ˆ„์ ํšŒ์ „์ˆ˜ ํ•„๋“œ์— ์ €์žฅํ•œ๋‹ค. 
		++accumulatedRotation;
		
		//๋ˆ„์  ํšŒ์ „์ˆ˜๊ฐ€ ์ตœ๋Œ€ ํšŒ์ „์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด (์ •์ƒ ํšŒ์ „(๋ˆ„์ <์ตœ๋Œ€)์ผ ๊ฒฝ์šฐ) ์‹คํ–‰ํ•œ๋‹ค.
		if(accumulatedRotation < maxRotation) {
			
			System.out.println(location + " Tire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation) + "ํšŒ");
			return true;//๋ฆฌํ„ดํƒ€์ž…๊ณผ returnํ‚ค์›Œ๋“œ ๋’ค์— ์žˆ๋Š” ํƒ€์ž…์ด ์ผ์น˜ํ•œ๋‹ค. 
			
		}else {//๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด = ๋ˆ„์  ํšŒ์ „์ˆ˜๊ฐ€ ์ตœ๋Œ€ ํšŒ์ „์ˆ˜์™€ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฉด (ํŽ‘ํฌ(๋ˆ„์ =์ตœ๋Œ€)์ผ ๊ฒฝ์šฐ) ์‹คํ–‰ํ•œ๋‹ค. 
			
			System.out.println("***" + location + " Tire ํŽ‘ํฌ ***");
			return false;
		}
	}
}

๋ฒˆ์™ธ) ์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ๋„ if, else๋ฌธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
if, else์— ๊ฐ๊ฐ result = true; result = false; ๋„ฃ๊ณ  return result;๋ฅผ ์“ด๋‹ค. 

	public boolean roll() {//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ public์ด๊ณ , ๋ฆฌํ„ดํƒ€์ž…์€ boolean์ธ roll์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ 
		//๋ˆ„์  ํšŒ์ „ ์ˆ˜ 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์„œ ๋ˆ„์ ํšŒ์ „์ˆ˜ ํ•„๋“œ์— ์ €์žฅํ•œ๋‹ค. 
		++accumulatedRotation;
		//๋ˆ„์  ํšŒ์ „์ˆ˜๊ฐ€ ์ตœ๋Œ€ ํšŒ์ „์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด (์ •์ƒ ํšŒ์ „(๋ˆ„์ <์ตœ๋Œ€)์ผ ๊ฒฝ์šฐ) ์‹คํ–‰ํ•œ๋‹ค.
		if(accumulatedRotation < maxRotation) {
			System.out.println(location + "Tire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation) + "ํšŒ");
			result = true;//๋ฆฌํ„ดํƒ€์ž…๊ณผ returnํ‚ค์›Œ๋“œ ๋’ค์— ์žˆ๋Š” ํƒ€์ž…์ด ์ผ์น˜ํ•œ๋‹ค. 
		}else {//๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด = ๋ˆ„์  ํšŒ์ „์ˆ˜๊ฐ€ ์ตœ๋Œ€ ํšŒ์ „์ˆ˜์™€ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฉด (ํŽ‘ํฌ(๋ˆ„์ =์ตœ๋Œ€)์ผ ๊ฒฝ์šฐ) ์‹คํ–‰ํ•œ๋‹ค. 
			System.out.println("***" + location + "Tire ํŽ‘ํฌ ***");
			result = false;
		}
		return result;
	}

2) Car ํด๋ž˜์Šค

์˜ˆ์ œ 2) Tire๋ฅผ ๋ถ€ํ’ˆ์œผ๋กœ ๊ฐ€์ง€๋Š” ํด๋ž˜์Šค Car.java

	int run() {//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ default์ด๊ณ , ๋ฆฌํ„ดํƒ€์ž…์ด int์ธ run์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ
	System.out.println("[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]");
	if(frontLeftTire.roll()==false) {stop(); return 1;}
	if(frontRightTire.roll()==false) {stop(); return 2;}
	if(backLeftTire.roll()==false) {stop(); return 3;}
	if(backRightTire.roll()==false) {stop(); return 4;}
	return 0; //์œ„ ์กฐ๊ฑด์ด ์•„๋‹Œ ๊ฒฝ์šฐ 0์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ๋„ฃ์–ด์ค€๋‹ค. 
	}

[1] Tire.java์˜ ๋ฉ”์„œ๋“œ๊ฐ€ return false;๊ฐ€ ๋  ๊ฒฝ์šฐ (false๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” roll()์ด ์žˆ์„ ๊ฒฝ์šฐ)
stop() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ•ด๋‹น ํƒ€์ด์–ด ๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 

package ch07_2;
//[2]
public class Car {
	//ํ•„๋“œ(์ฐธ์กฐ ํƒ€์ž…): ์ž๋™์ฐจ๋Š” 4๊ฐœ์˜ ํƒ€์ด์–ด๋ฅผ ๊ฐ€์ง„๋‹ค.
	Tire frontLeftTire = new Tire("์•ž์™ผ์ชฝ", 6); //new ๋’ค์— ์žˆ๋Š” ๊ฒƒ: ์ƒ์„ฑ์ž ํ˜ธ์ถœ
	Tire frontRightTire = new Tire("์•ž์˜ค๋ฅธ์ชฝ", 2);
	Tire backLeftTire = new Tire("๋’ค์™ผ์ชฝ", 3);
	Tire backRightTire = new Tire("๋’ค์˜ค๋ฅธ์ชฝ", 4);

	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ: ๋ชจ๋“  ํƒ€์ด์–ด๋ฅผ 1ํšŒ ํšŒ์ „์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๊ฐ Tire ๊ฐ์ฒด์˜ roll() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 
	//false๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” roll()์ด ์žˆ์„ ๊ฒฝ์šฐ stop()๋ฉ”์„œ๋“œ๋ฅผํ˜ธ์ถœํ•˜๊ณ  ํ•ด๋‹น ํƒ€์ด์–ด ๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 
	int run() {//์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ default์ด๊ณ , ๋ฆฌํ„ดํƒ€์ž…์ด int์ธ run์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ
	System.out.println("[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]");
	if(frontLeftTire.roll()==false) {stop(); return 1;}
	if(frontRightTire.roll()==false) {stop(); return 2;}
	if(backLeftTire.roll()==false) {stop(); return 3;}
	if(backRightTire.roll()==false) {stop(); return 4;}
	return 0; //์œ„ ์กฐ๊ฑด์ด ์•„๋‹Œ ๊ฒฝ์šฐ 0์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ๋„ฃ์–ด์ค€๋‹ค. 
	}

	//ํŽ‘ํฌ ๋‚ฌ์„ ๋•Œ ์‹คํ–‰: ์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ default์ด๊ณ , ๋ฆฌํ„ดํƒ€์ž…์ด void์ธ run์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ
	void stop() {//void์ด๋ฏ€๋กœ return์ด ์—†๋‹ค.
		System.out.println("[์ž๋™์ฐจ๊ฐ€ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.]");
	}
}


new๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด ์ˆ˜๋ฅผ ๊ณ„์† ๋งŒ๋“ ๋‹ค.

Tire frontLeftTire = new Tire("์•ž์™ผ์ชฝ", 6);
    0x100  
frontLeftTire 0x100 <--- Tire1  
    maxRotation 6
    accumulatedRotation 0
    location ์•ž์™ผ์ชฝ
Tire frontRightTire = new Tire("์•ž์˜ค๋ฅธ์ชฝ", 2);
    0x200  
frontRightTire 0x200 <--- Tire1  
    maxRotation 2
    accumulatedRotation 0
    location ์•ž์˜ค๋ฅธ์ชฝ
Tire backLeftTire = new Tire("๋’ค์™ผ์ชฝ", 3);
    0x300  
BackLeftTire 0x300 <--- Tire1  
    maxRotation 3
    accumulatedRotation 0
    location ๋’ค์™ผ์ชฝ
Tire backRightTire = new Tire("๋’ค์˜ค๋ฅธ์ชฝ", 4);
    0x400  
BackRightTire 0x400 <--- Tire1  
    maxRotation 4
    accumulatedRotation 0
    location ๋’ค์˜ค๋ฅธ์ชฝ

3) HankookTire, KumhoTire ํด๋ž˜์Šค

์ด ๋‘ ํด๋ž˜์Šค๋Š” Tire ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
์ƒ์„ฑ์ž๋Š” ํƒ€์ด์–ด์˜ ์œ„์น˜, ์ตœ๋Œ€ ํšŒ์ „์ˆ˜๋ฅผ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ๋ฐ›์•„์„œ ๋ถ€๋ชจ์ธ Tire ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค.
roll ๋ฉ”์„œ๋“œ๋Š” ์žฌ์ •์˜๋˜์–ด ์ •์ƒ ํšŒ์ „๊ณผ ํŽ‘ํฌ๊ฐ€ ๋‚ฌ์„ ๋•Œ ์ถœ๋ ฅํ•˜๋Š” ๋‚ด์šฉ์ด Tire ํด๋ž˜์Šค์˜ roll() ๋ฉ”์„œ๋“œ์™€ ๋‹ค๋ฅด๋‹ค.
(๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜(์˜ค๋ฒ„๋ผ์ด๋”ฉ)๋˜์—ˆ๋‹ค๋ฉด ์ž์‹ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋Œ€์‹  ํ˜ธ์ถœ๋œ๋‹ค.)
(์ถœ๋ ฅํ•˜๋Š” ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™๊ณ  ์ œํ’ˆ ์ด๋ฆ„์ด ์—†๋Š” Tire ํด๋ž˜์Šค์˜ roll() ๋ฉ”์„œ๋“œ ์ถœ๋ ฅ ๋‚ด์šฉ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค.)

System.out.println(location + "HankookTire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation)+"ํšŒ");
System.out.println("***" + location + "HankookTire ํŽ‘ํฌ ***");

System.out.println(location + "KumhoTire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation)+"ํšŒ");
System.out.println("***" + location + "KumhoTire ํŽ‘ํฌ ***");

์˜ˆ์ œ 3) Tire์˜ ์ž์‹ ํด๋ž˜์Šค HankookTire.java

package ch07_2;
//[3]
public class HankookTire extends Tire {
	//ํ•„๋“œ
	
	//์ƒ์„ฑ์ž
	public HankookTire(String location, int maxRotation) {
		super(location, maxRotation);
	}
	
	//๋ฉ”์„œ๋“œ: ๋‹ค๋ฅธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์žฌ์ •์˜ํ•œ roll() ๋ฉ”์„œ๋“œ
	public boolean roll() {
		++accumulatedRotation;
		if (accumulatedRotation < maxRotation) {
			System.out.println(location + " HankookTire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation)+"ํšŒ");
			return true;
		}else {
			System.out.println("***" + location + "HankookTire ํŽ‘ํฌ ***");
			return false;
		}
	}
}

์˜ˆ์ œ 4) Tire์˜ ์ž์‹ ํด๋ž˜์Šค KumhoTire.java

package ch07_2;
//[4]
public class KumhoTire extends Tire {
	//ํ•„๋“œ
	//์ƒ์„ฑ์ž
	public KumhoTire(String location, int maxRotation) {
		super(location, maxRotation);
	}
	
	//๋ฉ”์„œ๋“œ: ๋‹ค๋ฅธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์žฌ์ •์˜ํ•œ roll() ๋ฉ”์„œ๋“œ
	public boolean roll() {
		++accumulatedRotation;
		if (accumulatedRotation < maxRotation) {
			System.out.println(location + " KumhoTire ์ˆ˜๋ช…: "+(maxRotation-accumulatedRotation)+"ํšŒ");
			return true;
		}else {
			System.out.println("***" + location + "KumhoTire ํŽ‘ํฌ ***");
			return false;
		}
	}
}

4) CarExample ํด๋ž˜์Šค

๋ฉ”์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•œ๋‹ค. (CarExample)
์—ฌ๊ธฐ์— ๊ทธ ํ›„์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๋‚˜์—ดํ•œ๋‹ค. 

๋งˆ์ง€๋ง‰์œผ๋กœ CarExample ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด ์ง€๊ธˆ๊นŒ์ง€ ์ž‘์„ฑํ•œ Tire, Car, HankookTire, KumhoTire ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ์‹คํ–‰ํด๋ž˜์Šค์ด๋‹ค. 

์˜ˆ์ œ 5) ์‹คํ–‰ ํด๋ž˜์Šค CarExample.java

package ch07_2;
//[5]
public class CarExample {
	public static void main(String[] args) {
		//Car ๊ฐ์ฒด ์ƒ์„ฑ
		Car car = new Car(); 
		
		//Car ๊ฐ์ฒด์˜ run() ๋ฉ”์„œ๋“œ 5๋ฒˆ ๋ฐ˜๋ณต ์‹คํ–‰
		for(int i=1; i<=5; i++) {
			int problemLocation = car.run();
			
		switch(problemLocation) {
		case 1: //์•ž์™ผ์ชฝ ํƒ€์ด์–ด๊ฐ€ ํŽ‘ํฌ ๋‚ฌ์„ ๋•Œ HankookTire๋กœ ๊ต์ฒด
			System.out.println("์•ž์™ผ์ชฝ HankookTire๋กœ ๊ต์ฒด");
			car.frontLeftTire = new HankookTire("์•ž์™ผ์ชฝ", 15);
			break;
		case 2: //์•ž์˜ค๋ฅธ์ชฝ ํƒ€์ด์–ด๊ฐ€ ํŽ‘ํฌ ๋‚ฌ์„ ๋•Œ KumhoTire๋กœ ๊ต์ฒด
			System.out.println("์•ž์˜ค๋ฅธ์ชฝ KumhoTire๋กœ ๊ต์ฒด");
			car.frontRightTire = new HankookTire("์•ž์™ผ์ชฝ", 13);
			break;
		case 3: //๋’ค์™ผ์ชฝ ํƒ€์ด์–ด๊ฐ€ ํŽ‘ํฌ ๋‚ฌ์„ ๋•Œ HankookTire๋กœ ๊ต์ฒด
			System.out.println("๋’ค์™ผ์ชฝ HankookTire๋กœ ๊ต์ฒด");
			car.backLeftTire = new HankookTire("๋’ค์™ผ์ชฝ", 14);
			break;
		case 4: //๋’ค์˜ค๋ฅธ์ชฝ ํƒ€์ด์–ด๊ฐ€ ํŽ‘ํฌ ๋‚ฌ์„ ๋•Œ KumhoTire๋กœ ๊ต์ฒด
			System.out.println("๋’ค์˜ค๋ฅธ์ชฝ KumhoTire๋กœ ๊ต์ฒด");
			car.backRightTire = new KumhoTire("์•ž์™ผ์ชฝ", 17);
			break;
		}
		//1ํšŒ์ „ ์‹œ ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์„ ๊ตฌ๋ถ„ํ•œ๋‹ค.
		System.out.println("------------------------------");
		}
	}
}

๊ฒฐ๊ณผ)

[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]
์•ž์™ผ์ชฝ Tire ์ˆ˜๋ช…: 5ํšŒ
์•ž์˜ค๋ฅธ์ชฝ Tire ์ˆ˜๋ช…: 1ํšŒ
๋’ค์™ผ์ชฝ Tire ์ˆ˜๋ช…: 2ํšŒ
๋’ค์˜ค๋ฅธ์ชฝ Tire ์ˆ˜๋ช…: 3ํšŒ
------------------------------
[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]
์•ž์™ผ์ชฝ Tire ์ˆ˜๋ช…: 4ํšŒ
***์•ž์˜ค๋ฅธ์ชฝ Tire ํŽ‘ํฌ ***
[์ž๋™์ฐจ๊ฐ€ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.]
์•ž์˜ค๋ฅธ์ชฝ KumhoTire๋กœ ๊ต์ฒด
------------------------------
[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]
์•ž์™ผ์ชฝ Tire ์ˆ˜๋ช…: 3ํšŒ
์•ž์™ผ์ชฝ HankookTire ์ˆ˜๋ช…: 12ํšŒ
๋’ค์™ผ์ชฝ Tire ์ˆ˜๋ช…: 1ํšŒ
๋’ค์˜ค๋ฅธ์ชฝ Tire ์ˆ˜๋ช…: 2ํšŒ
------------------------------
[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]
์•ž์™ผ์ชฝ Tire ์ˆ˜๋ช…: 2ํšŒ
์•ž์™ผ์ชฝ HankookTire ์ˆ˜๋ช…: 11ํšŒ
***๋’ค์™ผ์ชฝ Tire ํŽ‘ํฌ ***
[์ž๋™์ฐจ๊ฐ€ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.]
๋’ค์™ผ์ชฝ HankookTire๋กœ ๊ต์ฒด
------------------------------
[์ž๋™์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.]
์•ž์™ผ์ชฝ Tire ์ˆ˜๋ช…: 1ํšŒ
์•ž์™ผ์ชฝ HankookTire ์ˆ˜๋ช…: 10ํšŒ
๋’ค์™ผ์ชฝ HankookTire ์ˆ˜๋ช…: 13ํšŒ
๋’ค์˜ค๋ฅธ์ชฝ Tire ์ˆ˜๋ช…: 1ํšŒ
------------------------------

3) ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ p. 345

void add(int x){

}
add(10)
//          ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ฐธ์กฐ ํƒ€์ž…
void add(Car x){ 

}

//     ์ž์‹ ํด๋ž˜์Šค์˜ ์ฐธ์กฐ ํƒ€์ž… (์ƒ์†์ด ๋˜์–ด์•ผ ๊ฐ€๋Šฅํ•˜๋‹ค.)
add(HankookTire ht);
add(KumhoTire ht);

์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜์€ ํ•„๋“œ์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•  ๋•Œ์—๋„ ๋ฐœ์ƒํ•˜์ง€๋งŒ, ์ฃผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งŽ์ด ๋ฐœ์ƒํ•œ๋‹ค.
๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ์—๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…๊ณผ ๋™์ผํ•œ ๋งค๊ฐœ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ •์„์ด์ง€๋งŒ,
๋งค๊ฐœ๊ฐ’์„ ๋‹ค์–‘ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ ๋ณ€์ˆ˜์— ์ž์‹ ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

ex) Driver ํด๋ž˜์Šค์—๋Š” drive() ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š”๋ฐ Vehicle ํƒ€์ž…์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋˜์–ด ์žˆ๋‹ค.
drive() ๋ฉ”์„œ๋“œ๋ฅผ ์ •์ƒ์ ์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

class Driver{
	void drive(Vehicle vehicle)
		vehicle.run();
    }
}

//์ฐธ์กฐ ํƒ€์ž…
Driver driver = new Driver();

//์ฐธ์กฐ ํƒ€์ž…
Vehicle vehicle = new Vehicle();

* Driver ํด๋ž˜์Šค์™€ Vehicle ํด๋ž˜์Šค ๊ฐ„์˜ ์ƒ์†์ด ์•„๋‹Œ ๊ฒฝ์šฐ *
driver.drive(vehicle);(O) //driver ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…๊ณผ ์ธ์ˆ˜์˜ ํƒ€์ž… ์ผ์น˜ 
driver.drive(driver);(X) //driver ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…๊ณผ ์ธ์ˆ˜์˜ ํƒ€์ž… ๋ถˆ์ผ์น˜

void drive(Vehicle vehicle)๊ณผ Vehicle ์ผ์น˜, Driver ๋ถˆ์ผ์น˜ 
๊ฐ™์€ ํƒ€์ž…์ด๋ฉด ๋œ๋‹ค. ์ฐธ์กฐ ๋ณ€์ˆ˜๋Š” ์ƒ์†์„ ํ†ตํ•ด์„œ ๋œ๋‹ค. (์ƒ์†์„ ํ•˜๋ฉด ๋‹คํ˜•์„ฑ์ด ์•ˆ ๋˜๋ฏ€๋กœ ์œ„์˜ ์ฝ”๋“œ ์˜ˆ์‹œ์™€ ๊ฐ™๋‹ค.)

class Calculator{
          void add(int x){}
}
Car car = new Car();

Calculator cal = new Calculator();
cal.add(10) (O)
cal.add(10.5) (X)

//๊ธฐ๋ณธ ํƒ€์ž…
int a = 10;

//๊ธฐ๋ณธ ํƒ€์ž…
float b = 10.5; 

cal.add(a) (O) //๊ฐ™์€ int ํƒ€์ž…์ด๋ฏ€๋กœ ๋œ๋‹ค. 
cal.add(b) (X)

๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ํด๋ž˜์Šค์ผ ๊ฒฝ์šฐ, ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ž์‹ ๊ฐ์ฒด๊นŒ์ง€๋„ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ–ˆ๋‹ค๋ฉด ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์–‘ํ•ด์ง„๋‹ค.

์ƒ์†๊ด€๊ณ„ X -> ๋‹คํ˜•์„ฑ X
Vehicle vehicle = new Bus();๋Š” Driver.java์™€ DriverExample.java์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค. 

์˜ˆ์ œ 1) ๋ถ€๋ชจ ํด๋ž˜์Šค Vehicle.java

package ch07_2;
//[1]
public class Vehicle {
	public void run() {
		System.out.println("์ฐจ๋Ÿ‰์ด ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ 2) Vehicle์„ ์ด์šฉํ•˜๋Š” ํด๋ž˜์Šค Driver.java

package ch07_2;
//[2]
public class Driver {
	//					int		a
	//					ํƒ€์ž… 		๋ณ€์ˆ˜๋ช…(๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.)
	//public void driver(int a) {
	public void driver(Vehicle vehicle) {
		vehicle.run();
	}
}

์˜ˆ์ œ 3) ์ž์‹ ํด๋ž˜์Šค Bus.java

package ch07_2;
//[3]
public class Bus extends Vehicle {
	//public class Bus{
	//๋ฉ”์„œ๋“œ ์žฌ์ •์˜(์˜ค๋ฒ„๋ผ์ด๋”ฉ)
	public void run() {
		System.out.println("๋ฒ„์Šค๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ 4) ์ž์‹ ํด๋ž˜์Šค Taxi.java

package ch07_2;
//[4]
public class Taxi extends Vehicle {
	//public class Taxi{
	//๋ฉ”์„œ๋“œ ์žฌ์ •์˜(์˜ค๋ฒ„๋ผ์ด๋”ฉ)
	public void run() {
		System.out.println("ํƒ์‹œ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ 5) ์‹คํ–‰ ํด๋ž˜์Šค DriverExample.java

package ch07_2;
//[5]
public class DriverExample {
	public static void main(String[] args) {
		Driver d = new Driver();
		Bus bus = new Bus();
		Taxi taxi = new Taxi();
		//Vehicle vehicle = new Vehicle();
		//d.driver(10);
		d.driver(bus); //Vehicle ํด๋ž˜์Šค์™€ Bus ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๊ด€๊ณ„๊ฐ€ ์•„๋‹ˆ๋ฉด X ์ž๋™ํƒ€์ž…๋ณ€ํ™˜: Vehicle vehicle = bus;
		d.driver(taxi); //Vehicle ํด๋ž˜์Šค์™€ Taxi ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๊ด€๊ณ„๊ฐ€ ์•„๋‹ˆ๋ฉด X ์ž๋™ํƒ€์ž…๋ณ€ํ™˜: Vehicle vehicle = taxi;
		//d.driver(vehicle); 
	}
}

๊ฒฐ๊ณผ) 

๋ฒ„์Šค๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.
ํƒ์‹œ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.

4) ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜(Casting) p. 348

๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜: ๋ถ€๋ชจ ํƒ€์ž…์„ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ
์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋œ ์ƒํƒœ์—์„œ ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•  ๋•Œ
์บ์ŠคํŒ…() ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž์‹ํƒ€์ž… ๋ณ€์ˆ˜ = (์ž์‹ํƒ€์ž…) ๋ถ€๋ชจํƒ€์ž…; //๋ถ€๋ชจ ํƒ€์ž…์„ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜


ex) ์ฐธ์กฐ ํƒ€์ž…: ์ž์‹ ํด๋ž˜์Šค ← (์ž์‹ ํด๋ž˜์Šค) ๋ถ€๋ชจ ํด๋ž˜์Šค

์˜ˆ์ œ) p.355 3๋ฒˆ ๋ฌธ์ œ
A ๋ถ€๋ชจ ํด๋ž˜์Šค, B ์ž์‹ ํด๋ž˜์Šค์ธ ์ƒํ™ฉ์—์„œ 
A a = new B(); //์„ ํ–‰ ํ›„
B b = (B) a; ๊ฐ€๋Šฅํ•˜๋‹ค. 

byte → short → char → int → long → float → double
int a = 10;
//8 <- 4
long b = a; (์ž๋™)

char c = 'A'
//4 <- 2
int d = c; (์ž๋™)

int e = 65;
//2 <- 4
char d = (char)e; (๊ฐ•์ œ)

์˜ˆ์ œ 1) ๋ถ€๋ชจ ํด๋ž˜์Šค Parent.java

package ch07_2;

public class Parent1 {
	public String field1;
	
	public void method1() {
		System.out.println("Parent-method1()");
	}
	
	public void method2() {
		System.out.println("Parent-method1()");
	}
}

 

์˜ˆ์ œ 2) ์ž์‹ ํด๋ž˜์Šค Child.java

package ch07_2;

public class Child1 extends Parent1 {
	public String field2; 
	
	public void method3() {
		System.out.println("Child-method3()");
	}
}

 

์˜ˆ์ œ 3) ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ ChildExample.java

package ch07_2;

public class ChildExample1 {
	public static void main(String[] args) {
	//์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜(์ฐธ์กฐ ํƒ€์ž…: ๋ถ€๋ชจํด๋ž˜์Šค <- ์ž์‹ ํด๋ž˜์Šค)
	Parent1 parent = new Child1();
	parent.field1 = "data1"; //Parent1.java์˜ filed1 
	parent.method1(); //๋ถ€๋ชจ ํด๋ž˜์Šค์— ์žˆ๋‹ค.
	parent.method2(); //๋ถ€๋ชจ ํด๋ž˜์Šค์— ์žˆ๋‹ค. 
	
	//parent.field2 = "data2"; parent์— field2๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์‚ฌ์šฉX
	//parent.method3(); ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์—†๋‹ค. 
	
	//๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜(์ฐธ์กฐ ํƒ€์ž…: ์ž์‹ ํด๋ž˜์Šค <- (์ž์‹ ํด๋ž˜์Šค) ๋ถ€๋ชจ ํด๋ž˜์Šค)
	Child1 child = (Child1) parent;
	child.field2 = "yyy";
	child.method3();
	}
}

๊ฒฐ๊ณผ)

Parent-method1()
Parent-method1()
Child-method3()

5) ๊ฐ์ฒด ํƒ€์ž… ํ™•์ธ (instanceof)

๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜์€ ์ž์‹ ํƒ€์ž…์ด ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค. 

Parent parent = new Parent();
Child child = (Child) parent; //๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ X

๋ถ€๋ชจ ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ๊ฐ์ฒด์ธ์ง€ ์ž์‹ ๊ฐ์ฒด์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•: instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
(์—ฐ์‚ฐ์ž: ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋˜๋Š” ํ‘œ์‹œ๋‚˜ ๊ธฐํ˜ธ)

์—ฐ์‚ฐ์ž์˜ ์ขŒํ•ญ์—๋Š” ๊ฐ์ฒด๊ฐ€ ์˜ค๊ณ  ์šฐํ•ญ์—๋Š” ํƒ€์ž…์ด ์˜ค๋Š”๋ฐ, ์ขŒํ•ญ์˜ ๊ฐ์ฒด๊ฐ€ ์šฐํ•ญ์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉด, (์ฆ‰ ์šฐํ•ญ์˜ ํƒ€์ž…์œผ๋กœ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค๋ฉด) true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 

boolean result = ์ขŒํ–ฅ(๊ฐ์ฒด) instanceof ์šฐํ–ฅ(ํƒ€์ž…)

instanceof ์—ฐ์‚ฐ์ž๋Š” ์ฃผ๋กœ ๋งค๊ฐœ๊ฐ’์˜ ํƒ€์ž…์„ ์กฐ์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ๋งค๊ฐœ๊ฐ’์ด ์–ด๋–ค ๊ฐ์ฒด์ธ์ง€ instanceof ์—ฐ์‚ฐ์ž๋กœ ํ™•์ธํ•˜๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜์„ ํ•ด์•ผํ•œ๋‹ค. 
๋งŒ์•ฝ ํƒ€์ž…์„ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜์„ ์‹œ๋„ํ•œ๋‹ค๋ฉด ClassCastException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.


์ด๋Ÿฌํ•œ ๋‹คํ˜•์„ฑ์œผ๋กœ ์ธํ•ด ๋Ÿฐํƒ€์ž„์— ์ฐธ์กฐ ๋ณ€์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…์„ ํ™•์ธํ•  ํ•„์š”์„ฑ์ด ์ƒ๊ธด๋‹ค. ์ž๋ฐ”์—์„œ๋Š” instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•˜์—ฌ, ์ฐธ์กฐ ๋ณ€์ˆ˜๊ฐ€ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ์‹ค์ œ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

์ž๋ฐ”์—์„œ instanceof ์—ฐ์‚ฐ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

์ฐธ์กฐ๋ณ€์ˆ˜ instanceof ํด๋ž˜์Šค์ด๋ฆ„

์™ผ์ชฝ์— ์ „๋‹ฌ๋œ ์ฐธ์กฐ ๋ณ€์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…์ด ์˜ค๋ฅธ์ชฝ์— ์ „๋‹ฌ๋œ ํด๋ž˜์Šค ํƒ€์ž…์ด๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ์— ์ฐธ์กฐ ๋ณ€์ˆ˜๊ฐ€ null์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ 1) ๋ถ€๋ชจ ํด๋ž˜์Šค

public class Parent {
}

์˜ˆ์ œ 2) ์ž์‹ ํด๋ž˜์Šค

public class Child extends Parent{
}

์˜ˆ์ œ 3) ๊ฐ์ฒด ํƒ€์ž… ํ™•์ธ

public class InstanceofExample {
	public static void method1(Parent parent) { //static: new ์•ˆ ์“ฐ๊ณ  ํ˜ธ์ถœํ•˜๋ ค๊ณ  ์“ด๋‹ค. 
		if(parent instanceof Child) {//๊ฒ€์ฆ: ๋งž์œผ๋ฉด true, ํ‹€๋ฆฌ๋ฉด false
			Child child = (Child) parent;
			System.out.println("method1 - Child๋กœ ๋ณ€ํ™˜ ์„ฑ๊ณต");
		} else {
			System.out.println("method1 - Child๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์Œ");
		}
	}

	public static void method2(Parent parent) {//๊ฒ€์ฆX
			Child child = (Child) parent; //์—๋Ÿฌ, ์˜ˆ์™ธ์ฒ˜๋ฆฌ
			System.out.println("method1 - Child๋กœ ๋ณ€ํ™˜ ์„ฑ๊ณต");
	}
	
	public static void main(String[] args) { //๋จผ์ € ์‹คํ–‰
		//๋ถ€๋ชจ ํด๋ž˜์Šค 		<- ์ž์‹ ํด๋ž˜์Šค
		Parent parentA = new Child();
		method1(parentA); //๊ฐ™์€ ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ์ด๋ฏ€๋กœ ํด๋ž˜์Šค ์ด๋ฆ„ ์ƒ๋žตํ•จ.
		method2(parentA);
	
		Parent parentB = new Parent();
		method1(parentB); //false๊ฐ’ ์‹คํ–‰: "Child๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์Œ"
		method2(parentB); //์˜ˆ์™ธ ๋ฐœ์ƒ
	}
}

๊ฒฐ๊ณผ)

method1 - Child๋กœ ๋ณ€ํ™˜ ์„ฑ๊ณต
method1 - Child๋กœ ๋ณ€ํ™˜ ์„ฑ๊ณต
method1 - Child๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์Œ
Exception in thread "main" java.lang.ClassCastException: ch07_2_5.Parent cannot be cast to ch07_2_5.Child
	at ch07_2_5.InstanceofExample.method2(InstanceofExample.java:14)
	at ch07_2_5.InstanceofExample.main(InstanceofExample.java:25)

๋งˆ๋ฌด๋ฆฌ p. 353
5๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ

1) ํด๋ž˜์Šค ํƒ€์ž… ๋ณ€ํ™˜: ๋‹ค๋ฅธ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

2) ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜: ์ž์‹ ๊ฐ์ฒด๋ฅผ ๋ถ€๋ชจ ํƒ€์ž… ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•  ๋•Œ์—๋Š” ์ž๋™์œผ๋กœ ํƒ€์ž…์ด ๋ณ€ํ™˜๋œ๋‹ค.
(์ฐธ์กฐ ํƒ€์ž…: ๋ถ€๋ชจํด๋ž˜์Šค ์ž์‹ ํด๋ž˜์Šค)


3) ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜
: ์ž์‹ ๊ฐ์ฒด๊ฐ€ ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋œ ์ƒํƒœ์—์„œ ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•  ๋•Œ ์บ์ŠคํŒ…() ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ (์ฐธ์กฐ ํƒ€์ž…: ์ž์‹ ํด๋ž˜์Šค ← (์ž์‹ ํด๋ž˜์Šค) ๋ถ€๋ชจ ํด๋ž˜์Šค)

์˜ˆ์ œ) p.355 3๋ฒˆ ๋ฌธ์ œ
A ๋ถ€๋ชจ ํด๋ž˜์Šค, B ์ž์‹ ํด๋ž˜์Šค์ธ ์ƒํ™ฉ์—์„œ 
A a = new B(); //์„ ํ–‰ ํ›„
B b = (B) a; ๊ฐ€๋Šฅํ•˜๋‹ค. 


4) instanceof ์—ฐ์‚ฐ์ž
: ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ์กฐ์‚ฌํ•  ๋•Œ instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ๋กœ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜ ์ „์— ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•œ์ง€ ์กฐ์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.


5) ๋‹คํ˜•์„ฑ
: ๊ฐ์ฒด ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋™์ผํ•˜์ง€๋งŒ ์‹คํ–‰๊ฒฐ๊ณผ๊ฐ€ ๋‹ค์–‘ํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ์„ฑ์งˆ์„ ๋งํ•œ๋‹ค.

๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ธฐ์ˆ ์€ 1. ๋ฉ”์„œ๋“œ ์žฌ์ •์˜์™€ 2. ํƒ€์ž… ๋ณ€ํ™˜์ด๋‹ค.
์ž์‹ ๊ฐ์ฒด๊ฐ€ ์žฌ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ, ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„์— ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์žฌ์ •์˜๋œ ์ž์‹ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.


//๋ถ€๋ชจ ํด๋ž˜์Šค
class Tire{
}
//์ž์‹ ํด๋ž˜์Šค
class HankookTire extends Tire{
}
//์ž์‹ ํด๋ž˜์Šค
class KumhoTire extends Tire{
}

class CarExample{
          public static void main(String[] args){
          //๋ถ€๋ชจํด๋ž˜์Šค = ์ž์‹ํด๋ž˜์Šค
                    Tire t = new HankookTire

Q2) ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์•Œ์•„๋ณด๊ธฐ
์ฝ”๋“œ 1)

public class Tire {
	public void run() {
		System.out.println("์ผ๋ฐ˜ ํƒ€์ด์–ด๊ฐ€ ๊ตด๋Ÿฌ๊ฐ‘๋‹ˆ๋‹ค.");
	}
}

์ฝ”๋“œ 2)

public class SnowTire extends Tire{
	//๋ฉ”์„œ๋“œ ์žฌ์ •์˜(์˜ค๋ฒ„๋ผ์ด๋”ฉ)
	public void run() {
		System.out.println("์Šค๋…ธ์šฐ ํƒ€์ด์–ด๊ฐ€ ๊ตด๋Ÿฌ๊ฐ‘๋‹ˆ๋‹ค.");
	}
}


์ฝ”๋“œ 3)

public class SnowTireExample {
	public static void main(String[] args) {
		SnowTire snowTire = new SnowTire();
		
		//์ฐธ์กฐ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜
		Tire tire = snowTire;
		
		snowTire.run();
		tire.run();
	}
}

๊ฒฐ๊ณผ)
์ž์‹ ๊ฐ์ฒด๊ฐ€ ์žฌ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ, ๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„์— ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์žฌ์ •์˜๋œ ์ž์‹ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

์Šค๋…ธ์šฐ ํƒ€์ด์–ด๊ฐ€ ๊ตด๋Ÿฌ๊ฐ‘๋‹ˆ๋‹ค.
์Šค๋…ธ์šฐ ํƒ€์ด์–ด๊ฐ€ ๊ตด๋Ÿฌ๊ฐ‘๋‹ˆ๋‹ค.


Q3) 

A ๋ถ€๋ชจ ํด๋ž˜์Šค, B ์ž์‹ ํด๋ž˜์Šค์ธ ์ƒํ™ฉ์—์„œ 
A a = new B(); //์„ ํ–‰ ํ›„
B b = (B) a; ๊ฐ€๋Šฅํ•˜๋‹ค.


Q5) service.login() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์„ ๊ตฌํ•˜๋ผ.
(๋ถ€๋ชจ) Service ← MemberService ← AService (์ž์‹)

์ „์ฒด ์ฝ”๋“œ)

์ „์ฒด ์ฝ”๋“œ

์ฝ”๋“œ 1) Service.java

package ch07_exercise;
//[1]
public class Service {
	public void login() {
		System.out.println("๋กœ๊ทธ์ธ");
	}
}


์ฝ”๋“œ 2) MemberSerive.java

package ch07_exercise;
//[2]
public class MemberService extends Service {
	public void login() {
		System.out.println("๋ฉค๋ฒ„ ๋กœ๊ทธ์ธ");
	}
}


์ฝ”๋“œ 3) AService.java

package ch07_exercise;
//[3]
public class AService extends MemberService {
	public void login() {
		System.out.println("A ๋กœ๊ทธ์ธ");
	}
}

์ฝ”๋“œ 4) ServiceExample.java

    0x100
controller 0x100 <--- Controller
    service
    public void setService(MemberService service) {
this.service = service;
}
package ch07_exercise;
//[4]
public class ServiceExample {
	public static void main(String[] args) {
		Controller controller = new Controller();
		
		controller.setA(10);
		
		controller.setService(new MemberService());
		controller.service.login();//์ถœ๋ ฅ ๊ฒฐ๊ณผ: ๋ฉค๋ฒ„ ๋กœ๊ทธ์ธ
		
		controller.setService(new AService());
		controller.service.login();//์ถœ๋ ฅ ๊ฒฐ๊ณผ: A ๋กœ๊ทธ์ธ
	}
}

์ฝ”๋“œ 5) Controller.java

์ „์ฒด ์ฝ”๋“œ

์ฝ”๋“œ [4]์™€ ํ•จ๊ป˜ ๋ณด๊ธฐ :)

int a = 10;
MemberService service = new MemberService()
MemberService service = new AService()
package ch07_exercise;
//[5]
public class Controller {
	public MemberService service;
	
	public void setA(int a) {
		this.a = a;
	}
	
	public void setService(MemberService service) {
		this.service = service;
	}
}

๊ฒฐ๊ณผ)

๋ฉค๋ฒ„ ๋กœ๊ทธ์ธ
A ๋กœ๊ทธ์ธ

728x90
๋ฐ˜์‘ํ˜•
Comments