Interface vs Abstract Class, Abstract method

Interface

  • ์„ค๋ช…

    • ๋ถ€๋ชจ, ์ž์‹ ๊ด€๊ณ„์ธ ์ƒ์† ๊ด€๊ณ„์— ์–ฝ๋งค์ด์ง€ ์•Š๊ณ , ๊ณตํ†ต ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค

      • Abstract Method ๋ฅผ ์ •์˜ํ•ด ๋†“๊ณ ,

      • ๊ตฌํ˜„ (implements) ํ•˜๋Š” Class ์—์„œ ๊ฐ ๊ธฐ๋Šฅ๋“ค์„ Overriding ํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋‹คํ˜•์„ฑ ๊ณผ ์—ฐ๊ด€๋˜์–ด ์žˆ๋‹ค

    • Interface๋Š” ํ•ด๋‹น Interface๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Class ๋“ค์— ๋Œ€ํ•ด ๋™์ผํ•œ method, ๋™์ž‘์„ ๊ฐ•์ œํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•œ๋‹ค

  • ์žฅ์ 

    • Java ์˜ ๋‹ค์ค‘ ์ƒ์†์ด ์•ˆ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” Abstract Class ์˜ ํ•œ๊ณ„๋ฅผ ๋ณด์™„ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค

  • ๋‹จ์ 

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

  • ์ •๋ฆฌ

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

Abstract method

  • ์„ค๋ช…

    • ์ž์‹ class์—์„œ ๋ฐ˜๋“œ์‹œ overriding ํ•ด์•ผ๋งŒ ํ•˜๋Š” method๋ฅผ ์˜๋ฏธํ•œ๋‹ค

    • method๋Š” ์„ ์–ธ๋ถ€ () ์™€ ๊ตฌํ˜„๋ถ€ {} ๋กœ ๋‚˜๋ˆ„์–ด ์ง€๋Š”๋ฐ, ์„ ์–ธ๋ถ€๊นŒ์ง€๋งŒ ์ž‘์„ฑํ•˜๊ณ  ๊ตฌํ˜„๋ถ€๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์€ method๋ฅผ ์˜๋ฏธํ•œ๋‹ค

    • method ๋‚ด์šฉ์ด ์ƒ์†๋ฐ›๋Š” class์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ƒ method๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

    • ๊ตฌํ˜„๋ถ€ {} ๋Š” ํ•ด๋‹น class๋ฅผ ์ƒ์† ๋ฐ›๋Š” ํ•˜์œ„ class์—์„œ ๋ฐ˜๋“œ์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค

      • ๊ตฌํ˜„๋ถ€๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค!

    • ์ฃผ์„์œผ๋กœ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” method ์ธ์ง€ ์„ค๋ช…ํ•ด์•ผ ํ•œ๋‹ค

    • overriding method์—๋Š” abstract ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค

Abstract Class

  • ์„ค๋ช…

    • Abstract method ๋ฅผ ํ•˜๋‚˜๋ผ๋„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” class๋ฅผ Abstract class ๋ผ๊ณ  ํ•œ๋‹ค

      • ์ฆ‰, ์ผ๋ฐ˜ class์—๋Š” ์ถ”์ƒ method๊ฐ€ ์žˆ์„ ์ˆ˜ ์—†๋‹ค!

    • Abstract class๋ฅผ ์ƒ์† ๋ฐ›์€ ์ž์‹ class๋Š” ๋ฐ˜๋“œ์‹œ abstract method ๋ฅผ overriding ํ•ด์•ผ ํ•œ๋‹ค

      • abstract class๊ฐ€ ์ž์‹ class์—๊ฒŒ abstract method์˜ ์žฌ์ •์˜๋ฅผ ๊ฐ•์š” ํ•œ๋‹ค

      • abstract class โ†” final class

        • overriding์„ ์ œํ•œํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์†์„ ๊ธˆ์ง€ ํ•˜๋Š” final class์™€ ๋ฐ˜๋Œ€๋œ๋‹ค!

    • Abstract class๋Š” ํ”„๋กœ๊ทธ๋žจ ์„ค๊ณ„ ๋ฅผ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค

      • ๋ฐ˜๋ฉด์—, ์ผ๋ฐ˜ class๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , data๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค

    • Abstract class๋Š” ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค

      • ์ž์‹ class์—์„œ abstract class๋ฅผ ์ƒ์† ๋ฐ›๊ณ , abstract method๋ฅผ overriding ํ•˜์—ฌ ๊ตฌํ˜„์„ ๋งˆ์นœ๋’ค, ์ž์‹ class๋ฅผ ์ด์š”ํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค

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

      • ๊ณตํ†ต ๊ธฐ๋Šฅ ์„ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•˜๊ณ ,

      • ํ™•์žฅ์‹œํ‚ค๋ฉฐ

      • ์ƒ์†๊ณผ ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค

        • ์ƒ์†์€ SuperClass์˜ ๊ธฐ๋Šฅ์„ ์ด์šฉ, ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค!

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

  • ์žฅ์ 

    • Abstract class์—์„œ ์„ค๊ณ„๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด, ์ž์‹ class์—์„œ ์ƒ์†์„ ๋ฐ›์•„ ๊ธฐ๋Šฅ์„ ํ™•์žฅ ํ•˜๋Š”๋ฐ ์šฉ์ดํ•˜๋‹ค

    • ์ž์‹ class์—์„œ abstract method์˜ ๊ตฌํ˜„์„ ๊ฐ•์š” ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ‘œ์ค€ํ™” ์ •๋„ ๋ฅผ ๋†’์ธ๋‹ค

    • class ๋“ค์˜ ๊ณตํ†ต ์‚ฌํ•ญ ์„ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ํŽธ๋ฆฌํ•ด์ง„๋‹ค

  • ๋‹จ์ 

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

    • ๋งŒ์•ฝ ๋‹ค์ค‘ ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด?

      class Vehicle extends Car, Motorcycle {
          @Override
          public void run(){
              super.drive();
          }
      }
      • Car, Motorcycle์— ๊ฐ๊ฐ drive() method๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ, ๋ฌด์—‡์„ ์ƒ์†๋ฐ›์•„ Overriding ํ•œ ๊ฒƒ์ธ์ง€ ๋ชจํ˜ธํ•ด ์ง„๋‹ค

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

Abstract Class vs Interface

  • Abstract Class๋Š” IS - A "~์ด๋‹ค" ์ด๊ณ ,

    • Interface๋Š” HAS - A "~์„ ํ•  ์ˆ˜ ์žˆ๋Š”" ์ด๋‹ค

  • Abstract Class๋Š” ์ƒ์† ์ด๊ณ ,

    • Interface๋Š” ๋‹คํ˜•์„ฑ ์ด๋‹ค

Last updated