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๐ŸŒณ๐Ÿ˜Š

[36] 230217 Java Ch. 7 ์ƒ์†: 3. ์ถ”์ƒ ํด๋ž˜์Šค [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 36์ผ] ๋ณธ๋ฌธ

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

[36] 230217 Java Ch. 7 ์ƒ์†: 3. ์ถ”์ƒ ํด๋ž˜์Šค [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 36์ผ]

yjyuwisely 2023. 2. 17. 12:49

230217 Fri 36th class

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

 

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

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

product.kyobobook.co.kr

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


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

- ์ธ์Šคํ„ด์Šค: ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด

- ์ƒ์„ฑ์ž constructor [6์žฅ 221์ชฝ]
: new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœ๋˜๋Š” ํŠน๋ณ„ํ•œ ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก. ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ, ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋˜์–ด ์žˆ๊ณ  ๋ฐ˜ํ™˜ํ˜•์ด ์—†๋‹ค. (๊ฐ์ฒด ์ดˆ๊ธฐํ™”๋ž€ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜, ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๋ฅผ ํ•˜ ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.)

- new๋Š” ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

- ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€ method signature [6์žฅ 247์ชฝ]
๋ฆฌํ„ดํ˜•, ๋ฉ”์†Œ๋“œ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ํฌํ•จํ•˜๋Š” ๊ฒƒ. ์˜๋ฌธ๋ช… ๊ทธ๋Œ€๋กœ ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ผ๊ณ ๋„ ํ•œ๋‹ค.

- ์–ด๋–ค ๊ฐ์ฒด์— ์†Œ์†๋˜์–ด์žˆ๋‹ค๋Š” ๋œป์€ new ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ์•ผ๋งŒ ํ•ด๋‹น ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด๋„ ๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฐธ๊ณ : https://jooona.tistory.com/m/164


Ch. 7 ์ƒ์†

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

7.3 ์ถ”์ƒ ํด๋ž˜์Šค 

์ถ”์ƒ(abstract): ์‹ค์ฒด ๊ฐ„์— ๊ณตํ†ต๋˜๋Š” ํŠน์„ฑ์„ ์ถ”์ถœํ•œ ๊ฒƒ์„ ๋งํ•œ๋‹ค. 
ex) ์ƒˆ, ๊ณค์ถฉ, ๋ฌผ๊ณ ๊ธฐ = ๋™๋ฌผ  
์œ„์˜ ์˜ˆ์‹œ๋Š” ๊ตฌ์ฒด์ ์ธ ์‹ค์ฒด X, ์‹ค์ฒด๋“ค์˜ ๊ณตํ†ต๋˜๋Š” ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ถ”์ƒ์ ์ธ ๊ฒƒ์ด๋‹ค.
(์‹ค์ฒด: ํ˜„์‹ค์— ์กด์žฌํ•˜๋Š” ๋ฌผ์ฒด)

์ถ”์ƒ ํด๋ž˜์Šค์™€ ์‹ค์ฒด ํด๋ž˜์Šค๋Š” ์ƒ์†์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 

์ถ”์ƒ ํด๋ž˜์Šค[๋ถ€๋ชจ]: ์œ„ ํด๋ž˜์Šค๋“ค์˜ ๊ณตํ†ต์ ์ธ ํŠน์„ฑ์„ ์ถ”์ถœํ•ด์„œ ์„ ์–ธํ•œ ํด๋ž˜์Šค
ํด๋ž˜์Šค๋“ค์˜ ๊ณตํ†ต์ ์ธ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”์ถœํ•ด์„œ ์„ ์–ธํ•œ ํด๋ž˜์Šค์ด๋‹ค.

์‹ค์ฒด ํด๋ž˜์Šค[์ž์‹]: ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค (์ด๋•Œ๊นŒ์ง€ ๋ฐฐ์šด ๊ฒƒ!) 
์‹ค์ฒด ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ํŠน์„ฑ์„ ๋ฌผ๋ ค๋ฐ›๊ณ , ์ถ”๊ฐ€์ ์ธ ํŠน์„ฑ(ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ)์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. 

abstract class ๋™๋ฌผ{
		void ์šด๋‹ค();
}

class ์†Œ{
		void ์šด๋‹ค(){
        		System.out.println("์Œ๋ฉ”")
        }
}
class ๊ฐ•์•„์ง€{
		void ์šด๋‹ค(){
        		System.out.println("๋ฉ๋ฉ")
        }
}
class ๊ณ ์–‘์ด{
		void ์šด๋‹ค(){
        		System.out.println("์•ผ์˜น")
        }
}

7.3.1 ์ถ”์ƒ ํด๋ž˜์Šค์˜ ์šฉ๋„ p. 358

1) ๊ณตํ†ต๋œ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ํ†ต์ผํ•  ๋ชฉ์ 

๊ณต๋™์œผ๋กœ ์ž‘์—…ํ–ˆ์„ ๋•Œ ๋ฉ”์„œ๋“œ, ๋ฆฌํ„ด ์ด๋ฆ„์ด ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
๊ตฌํ˜„์˜ ๋ชฉ์ ์ด ์•„๋‹ˆ๋‹ค. 

ex) Phone์ด๋ผ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์— ์†Œ์œ ์ž์ธ owner ํ•„๋“œ์™€ turnOn() ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜๊ณ , Telephone๊ณผ SmartPhone์€ Phone์„ ์ƒ์†ํ•จ์œผ๋กœ์จ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํ†ต์ผํ•  ์ˆ˜ ์žˆ๋‹ค. 

2) ์‹ค์ฒด ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์‹œ๊ฐ„ ์ ˆ์•ฝ

์„ค๊ณ„, ๊ตฌํ˜„์„ ๋”ฐ๋กœ ํ•จ์œผ๋กœ์จ ์‹œ๊ฐ„ ์ ˆ์•ฝ์ด ๋œ๋‹ค. ์—…๋ฌด ๋ถ„๋‹ด์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

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

ex) ํƒ€์ด์–ด ๊ทœ๊ฒฉ(์ถ”์ƒ ํด๋ž˜์Šค) 
ํ•œ๊ตญ ํƒ€์ด์–ด, ๊ธˆํ˜ธ ํƒ€์ด์–ด(์‹ค์ฒด ํด๋ž˜์Šค) 


7.3.2 ์ถ”์ƒ ํด๋ž˜์Šค ์„ ์–ธ

์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ์—๋Š” ํด๋ž˜์Šค ์„ ์–ธ์— abstract ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ธ๋‹ค. 
abstract๋ฅผ ๋ถ™์ด๋ฉด new ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ๋ชปํ•˜๊ณ , ์ƒ์†์„ ํ†ตํ•ด ์ž์‹ ํด๋ž˜์Šค๋งŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ƒ์„ฑ์ž๋Š” ์ƒ์†์ด ์•ˆ ๋œ๋‹ค. 
๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋งŒ super()์— ์˜ํ•ด ์ƒ์†์ด ๋œ๋‹ค.

์˜ˆ์ œ 1) ์ถ”์ƒ ํด๋ž˜์Šค

package ch07_3;

public class Phone {
	//ํ•„๋“œ
	public String owner;
	
	//์ƒ์„ฑ์ž
	public Phone(String owner) {
		this.owner = owner;
	}
	
	//๋ฉ”์„œ๋“œ
	public void turnOn() {
		System.out.println("ํฐ ์ „์›์„ ์ผญ๋‹ˆ๋‹ค.");
	}
	public void turnOff() {
		System.out.println("ํฐ ์ „์›์„ ๋•๋‹ˆ๋‹ค.");
	}
}

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

package ch07_3;

public class SmartPhone extends Phone{
	//์ƒ์„ฑ์ž
	public SmartPhone(String owner) {
		super(owner);
	}
	
	//๋ฉ”์„œ๋“œ
	public void internetSearch() {
		System.out.println("์ธํ„ฐ๋„ท ๊ฒ€์ƒ‰์„ ํ•ฉ๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ 3) ์‹คํ–‰ ํด๋ž˜์Šค

//Phone phone = new Phone();

 

Phone์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค€๋‹ค. 

package ch07_3;

public class PhoneExample {
	public static void main(String[] args) {
		//Phone phone = new Phone(); ์ถ”์ƒํด๋ž˜์Šค๋Š” ์„ค๊ณ„ ๋ชฉ์ ์ด๊ณ  ๊ตฌํ˜„ ๋ชฉ์ X, new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด ์ƒ์„ฑX
		
		SmartPhone smartPhone = new SmartPhone("ํ™๊ธธ๋™");
		
		smartPhone.turnOn();
		smartPhone.internetSearch();
		smartPhone.turnOff();
	}
}

7.3.3 ์ถ”์ƒ ๋ฉ”์„œ๋“œ์™€ ์žฌ์ •์˜ p. 362

์ถ”์ƒ ํด๋ž˜์Šค: ์‹ค์ฒด ํด๋ž˜์Šค๊ฐ€ ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ ธ์•ผ ํ•  ํ•„๋“œ, ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ด๋†“์€ ์ถ”์ƒ์ ์ธ ํด๋ž˜์Šค.
์‹ค์ฒด ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„(ํ•„๋“œ, ๋ฉ”์„œ๋“œ)๋ฅผ ํ†ต์ผํ•˜๋Š” ๋ชฉ์ ์ด ์žˆ๋‹ค.
๋ชจ๋“  ์‹ค์ฒด๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰ ๋‚ด์šฉ์ด ๋™์ผํ•˜๋‹ค๋ฉด ์ถ”์ƒ ํด๋ž˜์Šค์— ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ถ”์ƒ ๋ฉ”์„œ๋“œ๋Š” abstract ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ๋ฉ”์„œ๋“œ์˜ ์„ ์–ธ๋ถ€๋งŒ ์žˆ๊ณ  ๋ฉ”์„œ๋“œ ์‹คํ–‰ ๋‚ด์šฉ์ธ ์ค‘๊ด„ํ˜ธ {}๊ฐ€ ์—†๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค.

[public | protected ] abstract ๋ฆฌํ„ดํƒ€์ž… ๋ฉ”์„œ๋“œ์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜, ...);

์˜ˆ์‹œ) 

public abstract void sound();

์ถ”์ƒ ํด๋ž˜์Šค ์„ค๊ณ„ ์‹œ ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ๋ฐ˜๋“œ์‹œ ์‹คํ–‰ ๋‚ด์šฉ์„ ์ฑ„์šฐ๋„๋ก ๊ฐ•์ œํ•˜๊ณ  ์‹ถ์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค. 
์ž์‹ ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์„œ ์‹คํ–‰ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. (๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ)

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

abstract ๋ฐ˜ํ™˜ํƒ€์ž… ๋ฉ”์„œ๋“œ์ด๋ฆ„();
 
์œ„์™€ ๊ฐ™์ด ์„ ์–ธ๋ถ€๋งŒ ์žˆ๊ณ  ๊ตฌํ˜„๋ถ€๊ฐ€ ์—†๋‹ค๋Š” ์˜๋ฏธ๋กœ ์„ ์–ธ๋ถ€ ๋์— ๋ฐ”๋กœ ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ 1) ์ถ”์ƒ ๋ฉ”์„œ๋“œ ์„ ์–ธ Animal.java

package ch07_3;
//[1]
public abstract class Animal {
	//ํ•„๋“œ
	//๋™๋ฌผ์˜ ์ข…๋ฅ˜(ํฌ์œ ๋ฅ˜, ๊ณค์ถฉ)
	String kind;
	
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	//๋ชจ๋“ (๊ณค์ถฉ) ๋™๋ฌผ์€ ์ˆจ์„ ์‰ฝ๋‹ˆ๋‹ค. (๋ชจ๋“  ๋™๋ฌผ์ด ๋˜‘๊ฐ™์ด ์ˆจ์„ ์‰ฐ๋‹ค.)
	public void breath() {
		System.out.println("์ˆจ์„ ์‰ฝ๋‹ˆ๋‹ค.");
	}
	
	//๋ชจ๋“  ๋™๋ฌผ์€ ์†Œ๋ฆฌ๋ฅผ ๋ƒ…๋‹ˆ๋‹ค.(๋™๋ฌผ๋งˆ๋‹ค ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ๋‹ค๋ฅธ ์†Œ๋ฆฌ๋ฅผ ๋‚ธ๋‹ค.)
	public abstract void sound();
}

์˜ˆ์‹œ 2) ์ถ”์ƒ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ Dog.java

์ถ”์ƒ ํด๋ž˜์Šค์ธ Animal์„ ์ƒ์†ํ•˜๊ณ , ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ธ sound()๋ฅผ ์žฌ์ •์˜ํ–ˆ๋‹ค.

package ch07_3;
//[2]
public class Dog extends Animal {
	public Dog() {
		//super() (์ƒ๋žต๋จ) : ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋Š” ์ƒ์† ๋ฐ›๋Š”๋‹ค. 
		this.kind = "ํฌ์œ ๋ฅ˜";
	}

	@Override
	//์˜ค๋ฒ„๋ผ์ด๋”ฉ 
	public void sound() {
		System.out.println("๋ฉ๋ฉ");
	}
}

์˜ˆ์‹œ 3) ์ถ”์ƒ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ Cat.java

์ถ”์ƒ ํด๋ž˜์Šค์ธ Animal์„ ์ƒ์†ํ•˜๊ณ , ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ธ sound()๋ฅผ ์žฌ์ •์˜ํ–ˆ๋‹ค. 

package ch07_3;
//[3]
public class Cat extends Animal {
	public Cat() {
		this.kind = "ํฌ์œ ๋ฅ˜";
	}

	@Override
	//์˜ค๋ฒ„๋ผ์ด๋”ฉ 
	public void sound() {
		System.out.println("์•ผ์˜น");
	}
}

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

Dog์™€ Cat ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ sound() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค. 

package ch07_3;
//[4]
public class AnimalExample {
	public static void main(String[] args) {
		//Animal animal = new Animal(); ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ new๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑX
		Dog dog = new Dog();
		Cat cat = new Cat();
		dog.sound();
		cat.sound();
		System.out.println("----");
		
		//๋ณ€์ˆ˜์™€ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜
		//int 	a	  = 10;
		Animal animal = null; //์ฐธ์กฐํƒ€์ž…: ์ฃผ์†Œ๋ฅผ ์ €์žฅ, ๊ธฐ๋ณธ๊ฐ’:null
		//๋ถ€๋ชจ ํด๋ž˜์Šค (๋Œ€) = ์ž์‹ ํด๋ž˜์Šค (์†Œ) "์ž๋™ํƒ€์ž…๋ณ€ํ™˜"
		animal = new Dog();
		animal.breath();
		animal.sound();
		//๋ถ€๋ชจ ํด๋ž˜์Šค (๋Œ€) = ์ž์‹ ํด๋ž˜์Šค (์†Œ) "์ž๋™ํƒ€์ž…๋ณ€ํ™˜"
		animal = new Cat();
		animal.breath();
		animal.sound();
		System.out.println("----");
		
		//๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ
		animalSound(new Dog()); //animalSound: 2. ํ˜ธ์ถœ
		animalSound(new Cat());	
	}
	
	//์ƒˆ ๋ฉ”์„œ๋“œ ์„ ์–ธ 						Animal animal = new Dog()
	private static void animalSound(Animal animal) {//animalSound: 1. ์„ ์–ธ
		//static: new ํ‚ค์›Œ๋“œ ํ•„์š”X 
		//ํด๋ž˜์Šค์ด๋ฆ„.๋ฉ”์„œ๋“œ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ถœ
		animal.sound();
	}
}

์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

package ch07_3;
//[4]
public class AnimalExample {
	public static void main(String[] args) {
		//Animal animal = new Animal(); ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ new๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑX
		Dog dog = new Dog();
		Cat cat = new Cat();
		dog.sound();
		cat.sound();
		System.out.println("----");
		
		//๋ณ€์ˆ˜์™€ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜
		//int 	a	  = 10;
		Animal b = null; //์ฐธ์กฐํƒ€์ž…: ์ฃผ์†Œ๋ฅผ ์ €์žฅ, ๊ธฐ๋ณธ๊ฐ’:null
		//๋ถ€๋ชจ ํด๋ž˜์Šค (๋Œ€) = ์ž์‹ ํด๋ž˜์Šค (์†Œ) "์ž๋™ํƒ€์ž…๋ณ€ํ™˜"
		b = new Dog();
		b.breath();
		b.sound();
		//๋ถ€๋ชจ ํด๋ž˜์Šค (๋Œ€) = ์ž์‹ ํด๋ž˜์Šค (์†Œ) "์ž๋™ํƒ€์ž…๋ณ€ํ™˜"
		b = new Cat();
		b.breath();
		b.sound();
		System.out.println("----");
		
		//๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ
		animalSound(new Dog()); //animalSound: 2. ํ˜ธ์ถœ
		animalSound(new Cat());	
	}
	
	//์ƒˆ ๋ฉ”์„œ๋“œ ์„ ์–ธ 						Animal animal = new Dog()
	private static void animalSound(Animal c) {//animalSound: 1. ์„ ์–ธ
		//static: new ํ‚ค์›Œ๋“œ ํ•„์š”X 
		//ํด๋ž˜์Šค์ด๋ฆ„.๋ฉ”์„œ๋“œ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ถœ
		c.sound();
	}
}

๊ฒฐ๊ณผ)

๋ฉ๋ฉ
์•ผ์˜น
----
์ˆจ์„ ์‰ฝ๋‹ˆ๋‹ค.
๋ฉ๋ฉ
์ˆจ์„ ์‰ฝ๋‹ˆ๋‹ค.
์•ผ์˜น
----
๋ฉ๋ฉ
์•ผ์˜น

๋งˆ๋ฌด๋ฆฌ
2๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ
1) ์ถ”์ƒ ํด๋ž˜์Šค (abstract class)ํด๋ž˜์Šค๋“ค์˜ ๊ณตํ†ต์ ์ธ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”์ถœํ•ด์„œ ์„ ์–ธํ•œ ํด๋ž˜์Šค์ด๋‹ค.
[7์žฅ 357์ชฝ]
์‹ค์ฒด ํด๋ž˜์Šค๊ฐ€ ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ ธ์•ผ ํ•  ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋“ค์„ ์ •์˜ํ•ด ๋†“์€ ์ถ”์ƒ์ ์ธ ํด๋ž˜์Šค.
์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์‹ค์ฒด ํด๋ž˜์Šค์˜ ๊ณตํ†ต๋˜๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”์ถœํ•ด์„œ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.


2) ์ถ”์ƒ ๋ฉ”์„œ๋“œ (abstract method): ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ๋งŒ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฉ”์„œ๋“œ์˜ ์„ ์–ธ๋ถ€๋งŒ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค.
์ถ”์ƒ ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜๋˜์–ด ์‹คํ–‰ ๋‚ด์šฉ์„ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค.

์ฐธ๊ณ  ์šฉ์–ด: ์˜ค๋ฒ„๋ผ์ด๋”ฉ [7์žฅ 362์ชฝ]
ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ๋ฐ˜๋“œ์‹œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋„๋ก ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ.
๋ฉ”์„œ๋“œ์˜ ์„ ์–ธ๋ถ€๋งŒ ์žˆ๊ณ  ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ๋‚ด์šฉ์ธ ์ค‘๊ด„ํ˜ธ๊ฐ€ ์—†๋‹ค. ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค ์•ˆ์—์„œ๋งŒ ์„ ์–ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

public abstract class A{
          //์ถ”์ƒ ๋ฉ”์„œ๋“œ
          public abstract void add();
}
public class B extends A{
          //์ถ”์ƒ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
          public void add(){
          }

Q3) 
์ฝ”๋“œ 1)

public abstract class HttpServlet {
	//์ถ”์ƒ ๋ฉ”์„œ๋“œ
	public abstract void service(); //service ์ถ”์ƒ ๋ฉ”์„œ๋“œ
}


์ฝ”๋“œ 2)

public class LoginServlet extends HttpServlet{
	//service ์ถ”์ƒ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
	public void service() {
		System.out.println("๋กœ๊ทธ์ธ ํ•ฉ๋‹ˆ๋‹ค.");
	}
}

์ฝ”๋“œ 3)

public class FileDownloadServlet extends HttpServlet {
	//service ์ถ”์ƒ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
	public void service() {
		System.out.println("ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ํ•ฉ๋‹ˆ๋‹ค.");
	}
}

์ฝ”๋“œ 4)

public class HttpServletExample {
	public static void main(String[] args) {
		method(new LoginServlet());//๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค.
		method(new FileDownloadServlet()); //ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ์ž…๋‹ˆ๋‹ค.
	}//main ๋
	
	public static void method(HttpServlet servlet) {
		servlet.service();
	}
}

๊ธฐํƒ€)

HTML: ๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํ™”๋ฉด
Java:

 //BoardService์—์„œ ์„ค๊ณ„๋˜์–ด์ง„ wirte 
 public void write(BoardVO board){
 
 bm.write(board);
 
 (์ƒ๋žต)
 }

XML: BoardMapper.xml 

์•ฑ: ์ž๋ฐ”์˜ ์ƒ์† ์“ฐ์ž„,
์›น ๊ฐœ๋ฐœ์—๋Š” ์ƒ์† ์•ˆ ์“ฐ์ž„


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