๊ฐ์ฒด๋?
๊ฐ์ฒด ์งํฅ์ ๋ํด ์์๋ณด๊ธฐ ์ ์ "๊ฐ์ฒด"์ ๋ํด ์์๋ณด์. ๊ฐ์ฒด(ๅฎข้ซ)๋ผ๋ ๋จ์ด๋ฅผ ํ์๋ก ํ์ด๋ณด๋ฉด ์ ๊ฐ(ๅฎข) ์๋ฅผ ์ฐ๋ฉฐ '์๋'์ด๋ผ๋ ๋ป์ ๊ฐ์ง๊ณ ์๋ค. ๋ฐ๋๋ง์ ์ฃผ์ธ ์ฃผ(ไธป) ํ์๋ฅผ ์ฐ๋ "์ฃผ์ฒด"๋ผ๋ ๋จ์ด์ธ๋ฐ, ๊ฐ์ฒด๋ ์ฃผ์ฒด๊ฐ ์๋ ๊ฒ์ ์๋ฏธํ๋ฉฐ ์ฃผ์ฒด๊ฐ ํ์ฉํ๋ ๋์์ด๋ผ๋ ์๋ฏธ๋ค. ์ค์ ์ธ๊ณ์ ์กด์ฌํ๋ ๋ชจ๋ ์ฌ๋ฌผ์ด ๊ฐ์ฒด๊ฐ ๋ ์ ์๊ณ ์ด ๊ฐ์ฒด๋ ํ๋ก๊ทธ๋๋ฐ์ ๋์์ด ๋๋ค.
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (Object-Oriented Programming, OOP)
๊ฐ์ฒด ์งํฅ ์ด๋ก ์ ๊ธฐ๋ณธ ๊ฐ๋ ์ "์ค์ ์ธ๊ณ๋ ์ฌ๋ฌผ(๊ฐ์ฒด)๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ, ๋ฐ์ํ๋ ๋ชจ๋ ์ฌ๊ฑด๋ค์ ์ฌ๋ฌผ ๊ฐ์ ์ํธ์์ฉ์ด๋ค."๋ผ๊ณ ํํํ ์ ์๋๋ฐ, ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ด๋ฐ ๊ธฐ๋ณธ ๊ฐ๋ ์ ๋ฐํ์ผ๋ก ์ค์ ์ธ๊ณ๋ฅผ ์ปดํจํฐ ์์ ์ฎ๊ฒจ ๊ตฌํํ๋ ๊ฒ์ ๋งํ๋ค. ์ฆ, ์ค์ ์ธ๊ณ์ ๋ชจ๋ ๊ฒ๋ค์ ๊ฐ์ฒดํํ์ฌ ์ปดํจํฐ ์์ ํ๋ก๊ทธ๋๋ฐํ๋ ๊ฒ์ ๋งํ๋ค. ๋ธ๋ก ํํ์ ๋ชจ๋ํ ๋ ํ๋ก๊ทธ๋๋ฐ์ ํตํด ์ฝ๋ ๊ฐ์ ์๋ก ๊ด๊ณ๋ฅผ ๋งบ์ด ๋ณด๋ค ์ ๊ธฐ์ ์ผ๋ก ํ๋ก๊ทธ๋จ์ ๊ตฌ์ฑํ ์ ์๋ค.
๊ฐ์ฒด ์งํฅ ์ธ์ด์ ์ฅ์
โ
์ฝ๋ ์ฌ์ฌ์ฉ์ฑ ๋์
์๋ก์ด ์ฝ๋๋ฅผ ์์ฑํ ๋ ๊ธฐ์กด์ ์ฝ๋ ์ด์ฉํ์ฌ ์ฝ๊ฒ ์์ฑํ ์ ์์
โ
์ฝ๋์ ๊ด๋ฆฌ๊ฐ ์ฉ์ด
์ฝ๋ ๊ฐ์ ๊ด๊ณ๋ฅผ ์ด์ฉํด ์ ์ ๋
ธ๋ ฅ์ผ๋ก ์ฝ๊ฒ ์ฝ๋๋ฅผ ๋ณ๊ฒฝ ๊ฐ๋ฅ
โ
์ ๋ขฐ์ฑ ๋์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ๋ฅ
์ ์ด์์ ๋ฉ์๋๋ฅผ ์ด์ฉํด ๋ฐ์ดํฐ ๋ณดํธํ๊ณ ์ฝ๋์ ์ค๋ณต์ ์ ๊ฑฐํ์ฌ ์ฝ๋ ๋ถ์ผ์น๋ก ์ธํ ์ค๋์ ๋ฐฉ์ง
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํน์ง
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ 4๊ฐ์ง ํน์ง์ ๊ฐ์ง๊ณ ์๋๋ฐ ๊ฐ ํน์ง์ ์ ๊ธ์๋ฅผ ๋ฐ์ OOP is A PIE
๋ผ๊ณ ์ค์ฌ ๋ถ๋ฅด๊ธฐ๋ ํ๋ค.
1๏ธโฃ ์ถ์ํ(Abstraction)
์ถ์: ์ฌ๋ฌ ๊ฐ์ง ์ฌ๋ฌผ์ด๋ ๊ฐ๋ ์์ ๊ณตํต๋๋ ํน์ฑ์ด๋ ์์ฑ ๋ฐ์๋ฅผ ์ถ์ถํ์ฌ ํ์ ํ๋ ์์ฉ.
'์ถ์'์ด๋ ๋จ์ด์ ์ฌ์ ์ ์๋ฏธ๋ ์์ ๊ฐ์๋ฐ, ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ก ์๊ฐํด๋ณด๋ฉด ์ค์ ๋ก ์กด์ฌํ๋ ์ฌ๋ฌผ(๊ฐ์ฒด)์ ๊ณตํต๋ ํน์ง์ ๋ฌถ์ด ํ๋์ ํด๋์ค๋ก ์ ์ํ๋ ๊ฒ์ ์ถ์ํ๋ผ๊ณ ํ๋ค. ์๋ ๊ทธ๋ฆผ์ ์์์ฒ๋ผ ํ์ค ์ธ๊ณ์ ๊ฐ์์ง์ ์๋ผ๋ ๊ฐ์ฒด๊ฐ ์กด์ฌํ ๋, ์ด๋ค์ ๊ณตํต์ ์ธ ํน์ง์ด๋ ํ๋๋ค์ ์ถ์ถํ์ฌ Animal
์ด๋ผ๋ ํด๋์ค๋ก ์ ์ํ๋ ๊ฒ์ด ์ถ์ํ ๊ณผ์ ์ด๋ค. ์ด๋ ๊ฒ ์ ์๋ ํด๋์ค๋ ๊ตฌ์ฒดํ ๊ณผ์ ์ ๊ฑฐ์ณ ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด(instance)๊ฐ ๋์ด ํ์ฉ๋๋ค. ์ถ์ํ์์ ๊ฐ์ฒด์ ๊ณตํต๋ ์ํ์ ๊ธฐ๋ฅ์ ์ถ์ถํ๋ ๊ณผ์ ์ ํ๋ก๊ทธ๋จ์ ํน์ฑ์ ๋ง๊ฒ ๋ถํ์ํ ์ ๋ณด๋ ์จ๊ธฐ๊ณ ์ค์ํ ์ ๋ณด๋ง ๊ฐ์ ธ์ ์ ์ํ๋ค.
2๏ธโฃ ์์ (Inheritance)
๊ธฐ์กด์ ํด๋์ค๋ฅผ ์ฌ์ฌ์ฉํ์ฌ ์๋ก์ด ํด๋์ค๋ฅผ ์ ์ํ๋ ๊ฒ์ ๋งํ๋ค. ์์์ ์ด์ฉํ๋ฉด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ ์ฝ๋๋ฅผ ๊ณตํต์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฝ๋์ ์ถ๊ฐ ๋ฐ ๋ณ๊ฒฝ์ด ์ฉ์ดํ๋ค. ์๋ฐ์์๋ extneds
ํค์๋๋ฅผ ์ฌ์ฉํด ์์์ ๊ตฌํํ๋ฉฐ ๋ ํด๋์ค๊ฐ ์๋ก ์์ ๊ด๊ณ์ ์์ ๋ ์์ํด ์ฃผ๋ ํด๋์ค๋ฅผ ์กฐ์ ํด๋์ค(๋ถ๋ชจํด๋์ค)๋ผ๊ณ ํ๋ฉฐ ์์๋ฐ๋ ํด๋์ค๋ฅผ ์์ ํด๋์ค(์์ ํด๋์ค)๋ผ๊ณ ํ๋ค. ์์ ๊ด๊ณ๋ UML ํํ์ผ๋ก ์์ด ๋น ์ค์ ํ์ดํ๋ฅผ ์ฌ์ฉํ๋ค. ์์ ๊ด๊ณ๋ is a
๊ด๊ณ๋ผ๊ณ ๋ ํํํ๋๋ฐ, ๋ถ๋ชจ์ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ ์์์ด ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก "์์ is a ๋ถ๋ชจ"
๋ผ๊ณ ๋ํ๋ผ ์ ์๋ค.
3๏ธโฃ ๋คํ์ฑ (Polymorphism)
ํ๋์ ๊ฐ์ฒด๊ฐ ์ฌ๋ฌ ํํ(ํ์ )๋ฅผ ๊ฐ์ง ์ ์๋ ์ฑ์ง์ ๋งํ๋ค. ์๋ฐ์์๋ ํ ํ์ ์ ์ฐธ์กฐ๋ณ์๋ก ์ฌ๋ฌ ํ์ ์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋๋ก ํ์ฌ ๋คํ์ฑ์ ๊ตฌํํ์๋ค. ์ฆ, ์์ ๊ด๊ณ์ ์์ ๋ ์กฐ์ ํด๋์ค์ ์ฐธ์กฐ๋ณ์๋ก ์์ ํด๋์ค ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ค. ์ด๋ฐ ์ฑ์ง์ ์ด์ฉํ๋ฉด ์ฌ๋ฌ ์ข ๋ฅ์ ๊ฐ์ฒด๋ฅผ ํ๋์ ๋ฐฐ์ด๋ก ๋ค๋ฃฐ ์ ์๊ณ , ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ์ฌ๋ฌ ํ์ ์ ๊ฐ์ฒด๋ฅผ ๋ฐ์ ์ฌ ์ ์๋ค. ์ด๋ฟ๋ง ์๋๋ผ ๋ฉ์๋์ ์ค๋ฒ๋ก๋ฉ, ์ค๋ฒ๋ผ์ด๋ฉ์ ํตํด ๊ฐ์ ๋ชจ์์ ๋ฉ์๋๊ฐ ์ฌ๋ฌ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ํ๋ ๊ฒ๋ ๋คํ์ฑ์ ์์๋ก ๋ค ์ ์๋ค.
Student student = new Student(); //๊ฐ์ฒด ์์ฑ
Student s = student; // ๊ฐ์ ํ์
ํด๋์ค๋ก ๊ฐ์ฒด ์ฐธ์กฐ ๊ฐ๋ฅ
Person p = student; // ๋ถ๋ชจ ํ์
ํด๋์ค๋ก ๊ฐ์ฒด ์ฐธ์กฐ ๊ฐ๋ฅ
Object o = student; // ์กฐ์ ํ์
ํด๋์ค๋ก ๊ฐ์ฒด ์ฐธ์กฐ ๊ฐ๋ฅ
4๏ธโฃ ์บก์ํ, ๋ฐ์ดํฐ ์๋๊ณผ ๋ณดํธ (Encapsulation)
ํด๋์ค ๋ด๋ถ์ ๋ฉค๋ฒ ๋ณ์์ ๋ฉ์๋๋ฅผ ํ๋์ ์บก์๋ก ๋ง๋ค์ด ์ธ๋ถ๋ก๋ถํฐ ๋ณดํธํ๋ ๊ฒ์ ๋งํ๋ค. ๋ด๋ถ์ ์ ๋ณด๋ฅผ ์ธ๋ถ๋ก๋ถํฐ ๋ณดํธํ๋ ์ด์ ๋ ์ธ๋ถ์ ๋ ธ์ถ๋์ด์ ์ ๋๊ฑฐ๋ ๋ถํ์ํ ์ ๋ณด๋ค์ ๋ด๋ถ์ ์ผ๋ก๋ง ์ฌ์ฉํ๊ฒ ํ์ฌ ์ ๋ณด๋ฅผ ์๋ํ๊ธฐ ์ํจ์ด๋ค. ์๋ฐ์์ ์ ๊ทผ ์ ์ด์๋ฅผ ์ด์ฉํด ์ธ๋ถ๋ก๋ถํฐ์ ์ ๊ทผ์ ํ์์ ๋ฐ๋ผ ์ฐจ๋จํ๋ค.
์๋ฅผ ๋ค์ด, ํ ํด๋์ค์ ์๋ ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ธ๋ถ์์ ํจ๋ถ๋ก ์์ ํ ์ ์๋๋ก ํ๊ณ ์ถ๋ค๋ฉด, ํด๋น ๋ณ์๋ฅผ private
์ผ๋ก ์ค์ ํ์ฌ ์ ๊ทผ์ ํด๋์ค ๋ด๋ถ์์๋ง ๊ฐ๋ฅํ๊ฒ ๋ง๋ ๋ค. ๊ทธ๋ฐ ํ ์ธ๋ถ์์ ์ ๋ณด์ ์์ ์ด๋ ์กฐํ๊ฐ ํ์ํ๋ค๋ฉด ๊ณต๊ฐ๋ ํน์ ๋ฉ์๋(ex. setter
, getter
)๋ฅผ ํตํด์๋ง ๊ฐ๋ฅํ๊ฒ ํ์ฌ ๋ฌด๋ถ๋ณํ ์ ๊ทผ์ด ๋ถ๊ฐํ๋๋ก ์ ๋ณด๋ฅผ ๋ณดํธํ๋ค.
*์ฐธ๊ณ : Java์ ์ ์
'๐ Language > ๐ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๊ฐ๋น์ง ์ปฌ๋ ์ (Garbage Collection) / Minor GC, Major GC (0) | 2023.09.01 |
---|---|
[Java] Scanner ํด๋์ค / nextInt(), next(), nextLine() (0) | 2022.08.03 |
[Java] Arrays ๊ด๋ จ (0) | 2022.05.26 |
[Java] length, length(), size() (0) | 2022.05.21 |
[Java] ๋ด๊ฐ ๋ณด๋ ์๋ฐ ๋์ ๋์ .. (0) | 2022.05.06 |