[Java] Interface vs Abstract Class ์ •๋ฆฌ

GilLogยท2020๋…„ 11์›” 29์ผ
13

Java

๋ชฉ๋ก ๋ณด๊ธฐ
10/29

Interface vs Abstract Class

Interface์™€ Abstract Class๋Š” ์ƒ์†(extends)๋ฐ›๊ฑฐ๋‚˜, ๊ตฌํ˜„(implements)ํ•˜๋Š” Class๊ฐ€ Interface๋‚˜ Abstract Class ์•ˆ์— ์žˆ๋Š” Abstract Method๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ๊ฐ•์ œํ•˜๋Š” ๊ณตํ†ต์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด Interface์™€ Abstract Class ๋‘ ์ข…๋ฅ˜๊ฐ€ ์กด์žฌํ•˜๋Š”๊ฑด ์™œ์ผ๊นŒ?

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด, Interface์™€ Abstract Class๋Š” ์กด์žฌ ๋ชฉ์ ์ด ๋‹ค๋ฅด๋‹ค.

Interface

Interface๋Š” ๋ถ€๋ชจ, ์ž์‹ ๊ด€๊ณ„์ธ ์ƒ์† ๊ด€๊ณ„์— ์–ฝ๋ฉ”์ด์ง€ ์•Š๊ณ , ๊ณตํ†ต ๊ธฐ๋Šฅ์ด ํ•„์š” ํ• ๋•Œ, Abstract Method๋ฅผ ์ •์˜ํ•ด๋†“๊ณ  ๊ตฌํ˜„(implements)ํ•˜๋Š” Class์—์„œ ๊ฐ ๊ธฐ๋Šฅ๋“ค์„ Overridngํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋‹คํ˜•์„ฑ๊ณผ ์—ฐ๊ด€๋˜์–ด ์žˆ๋‹ค.

Interface๋Š” ํ•ด๋‹น Interface๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Class๋“ค์— ๋Œ€ํ•ด ๋™์ผํ•œ method, ๋™์ž‘์„ ๊ฐ•์ œํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•œ๋‹ค.
Java์—์„œ ๋‹ค์ค‘ ์ƒ์†์ด ์•ˆ๋˜์–ด ๋ฐœ์ƒํ•˜๋Š” Abstract Class์˜ ํ•œ๊ณ„๋„ ๋ณด์™„ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
Interface์˜ implements์—๋Š” ์ œํ•œ์ด ์—†์–ด ๋‹ค์ค‘ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ๋ชจ๋“  Class๊ฐ€ Interface๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด, ๊ณตํ†ต์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋„ implementsํ•˜๋Š” ๋ชจ๋“  Class์—์„œ Overrindgํ•ด ์žฌ์ •์˜ํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์กด์žฌํ•œ๋‹ค.

Interface๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ Abstract Class๋ฅผ ์ƒ์†ํ•˜๋Š” Class๋“ค์˜ ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ๋ช…์‹œํ• ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค.

Abstract Class

Abstract Class๋Š” ๋ถ€๋ชจ์™€ ์ž์‹ ์ฆ‰, ์ƒ์† ๊ด€๊ณ„์—์„œ Abstract Class๋ฅผ ์ƒ์†(extends)๋ฐ›์œผ๋ฉฐ ๊ฐ™์€ ๋ถ€๋ชจ Class(์—ฌ๊ธฐ์„œ๋Š” Abstract Class)๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ž์‹ Class๋“ค ๊ฐ„์— ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•˜๊ณ , ํ™•์žฅ์‹œํ‚ค๋ฉฐ ์ƒ์†๊ณผ ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค.
์ƒ์†์€ SuperClass์˜ ๊ธฐ๋Šฅ์„ ์ด์šฉ, ํ™•์žฅ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

Abstract Class๋Š” IS - A "~์ด๋‹ค"์ด๊ณ , Interface๋Š” HAS - A "~์„ ํ•  ์ˆ˜ ์žˆ๋Š”"์ด๋‹ค.

Abstract Class๋ฅผ ์ƒ์†ํ•˜๋ฉฐ Class๋“ค๊ฐ„์˜ ๊ตฌ๋ถ„์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

Java์—์„œ๋Š” ๋‹ค์ค‘ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Abstract Class ๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” Abstract Method๋ฅผ ๊ฐ•์ œํ•˜๋Š”๋ฐ๋Š” ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•œ๋‹ค.

  • Example
class Vehicle extends Car, Motorcycle {
    @Override
    public void run(){
        super.drive();
    }
}

๋งŒ์•ฝ Java์—์„œ ๋‹ค์ค‘ ์ƒ์†์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค๋ฉด Car, Motocycle์— ๊ฐ๊ฐ drive method๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฌด์—‡์„ ์ƒ์†๋ฐ›์•„ Overridng ํ•œ๊ฑด์ง€ ๋ชจํ˜ธํ•ด์ง„๋‹ค.

์ด๊ฒƒ์ด ๋‹ค์ค‘ ์ƒ์†์˜ ๋ชจํ˜ธ์„ฑ์ด๊ณ , ์ด ๋•Œ๋ฌธ์— Java๋Š” ๋‹ค์ค‘ ์ƒ์†์„ ๋ง‰์•„ ๋†“์•˜๋‹ค.

Interface vs Abstract Class ์˜ˆ์ œ

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜„์‹ค์„ธ๊ณ„์˜ ๊ฐ์ฒด๋“ค์„ Interface, Abstract Class๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ œ์ผ ๋ถ€๋ชจ Class์ธ Creature๋Š” ์ƒ๋ฌผ์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ๋‚ด๋ฆฌ๊ณ  ์žˆ๋Š” Abstract Class์ด๋‹ค.

์ด๋ฅผ ์ƒ์† ๋ฐ›๋Š” Animal(๋™๋ฌผ), Plant(์‹๋ฌผ) Abstact Class๋Š” ๊ฐ๊ฐ ๋™๋ฌผ, ์‹๋ฌผ์˜ ํŠน์„ฑ์„ abstarct method๋กœ ์ •์˜ํ•ด๋†“๋Š”๋‹ค.
๋™๋ฌผ(์›€์ง์ด๊ธฐ), ์‹๋ฌผ(๊ด‘ํ•ฉ์„ฑํ•˜๊ธฐ) ๋“ฑ..

Animal Class๋ฅผ ์ƒ์†๋ฐ›๋Š” Monkey, Kim, Gil์€ ๊ฐ๊ฐ ์›์ˆญ์ด, ์‚ฌ๋žŒ, ์‚ฌ๋žŒ์œผ๋กœ ์‚ฌ๋žŒ์€ ์›์ˆญ์ด์™€ ๋‹ค๋ฅด๊ฒŒ ๋”ฐ๋กœ ๋ง์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ(Talkable)์ด ์žˆ๊ณ , ์ด ์ค‘ Gil์ด๋ผ๋Š” ์‚ฌ๋žŒ Class๋Š” Program์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Plant Class๋ฅผ ์ƒ์†๋ฐ›๋Š” Venus Flytrap์€ ํŒŒ๋ฆฌ ์ง€์˜ฅ, ๋ˆ๋ˆ์ด ์ฃผ๊ฑฑ ์‹๋ฌผ๋กœ์„œ, ์‹๋ฌผ์ธ๋ฐ๋„ ๋จน๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋”ฐ๋ผ์„œ Eatable Interface๋ฅผ ์‚ฌ์šฉํ•ด Monkey, Kim, Gil, Venus Flaytrap ๋ชจ๋‘๊ฐ€ implementsํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ Interface์˜ ์‚ฌ์šฉ ๋ชฉ์ ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ, Eatable Interface๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ Animal, Plant ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ถ€๋ชจ Class๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š” Monkey, Kim, Gil, Venus Flaytrap์—๊ฒŒ ๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ์ธ ๋จน๋Š” ๊ธฐ๋Šฅ์„ Interface๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ƒ์† ๊ด€๊ณ„๊ฐ€ ๋‹ค๋ฆ„์—๋„ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋„๋ก ๊ฐ•์ œ ํ•  ์ˆ˜ ์žˆ๋‹ค.


Abstract Class์˜ ์‚ฌ์šฉ ๋ชฉ์ ์„ ์‚ดํŽด๋ณด๋ฉด, ์‚ฌ์‹ค ์œ„์— Talkable Interface๋Š” ๋™์ผํ•œ ๋ถ€๋ชจ Class์ธ Animal์„ ์ƒ์†๋ฐ›๋Š” Kim, Gil์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

์ด๋ฅผ ์ƒˆ๋กœ์šด Abstract Class๋ฅผ ์ƒ์„ฑํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด,

Animal Class๋ฅผ ์ƒ์†๋ฐ›๋Š” Human Class์— ๋งํ•˜๋Š” ๊ธฐ๋Šฅ์„ abstarct method๋กœ ์ •์˜ ํ•ด๋†“์Œ์œผ๋กœ์จ, ๊ฐ™์€ ๋ถ€๋ชจ class๋ฅผ ์ƒ์†๋ฐ›๋Š” ๊ด€๊ณ„์—์„œ ๊ณตํ†ต ๊ธฐ๋Šฅ์˜ ๊ตฌํ˜„์„ ๊ฐ•์ œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ ์˜ˆ์ œ์—์„œ Eatable Interface์™€ Human Abstarct Class๋ฅผ ์ž˜ ์ดํ•ดํ•œ๋‹ค๋ฉด, Interface์™€ Abstract Class์˜ ์ฐจ์ด์ ์˜ ์œค๊ณฝ์„ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ™†โ€โ™‚๏ธ ์ฐธ๊ณ ์‚ฌ์ดํŠธ ๐Ÿ™‡โ€โ™‚๏ธ

์ž๋ฐ”์˜ ์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค - ์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ฐจ์ด[by๊ฐ•๊ด€์šฐ]

[JAVA] ์ถ”์ƒํด๋ž˜์Šค VS ์ธํ„ฐํŽ˜์ด์Šค ์™œ ์‚ฌ์šฉํ• ๊นŒ? ์ฐจ์ด์ , ์˜ˆ์ œ๋กœ ํ™•์ธ [๋งˆ์ด์ž๋ชฝ myJamong]

์ถ”์ƒํ™”ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์šฉ๋„, ์ฐจ์ด์ , ๊ณตํ†ต์ [์‹ ๋งค๋ ฅ]

profile
๐Ÿš€ ๊ธฐ๋ก๋ณด๋‹จ ๊ธธ๋ก์„ 20.10 ~ 22.02 โญ Move To : https://gil-log.github.io/

3๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 2์›” 25์ผ

๋•๋ถ„์— ์ฐจ์ด์ ์„ ํ•œ ๋ฐฉ์— ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
์ค‘๊ฐ„์— Kim๊ณผ Gil์ด eatable์ด์–ด์„œ ์ˆœ๊ฐ„ ๋‹นํ™ฉํ–ˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2023๋…„ 7์›” 10์ผ

์‹ค์ œ๋กœ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ถ”์ƒํด๋ž˜์Šค์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ๋ชจ์˜๋ฉด์ ‘์„ ๋ณธ ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ, ์ œ๋Œ€๋กœ ๋œ ๋‹ต์„ ๋ชป ๋ƒˆ์—ˆ์Šต๋‹ˆ๋‹ค.
์ž‘์„ฑํ•˜์‹  ๊ธ€์„ ๋ณด๋ฉด์„œ ์ž˜ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ