ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 인터페이스(Interface)와 추상 클래스(Abstract class)의 차이
    C# 2023. 3. 5. 15:50

    인터페이스(Interface)와 추상 클래스(Abstract class)는 객체 지향 프로그래밍에서 다형성을 구현하는 방법 중 두 가지입니다.


    인터페이스 ( Interface )

    인터페이스는 클래스가 구현해야 하는 동작(메서드)의 목록을 정의합니다. 인터페이스를 사용하여 다른 클래스와의 상호 작용을 정의할 수 있습니다. 인터페이스는 다중 상속을 지원하므로 클래스가 여러 개의 인터페이스를 구현할 수 있습니다. 인터페이스는 클래스의 기본 동작을 제공하지 않으므로 인터페이스를 구현하는 클래스는 모든 메서드를 정의해야 합니다.

     

    // IAnimal 인터페이스 정의
    public interface IAnimal
    {
        void MakeSound();
    }
    
    // Dog 클래스, IAnimal 인터페이스 구현
    public class Dog : IAnimal
    {
        public void MakeSound()
        {
            Console.WriteLine("멍멍");
        }
    }
    
    // Cat 클래스, IAnimal 인터페이스 구현
    public class Cat : IAnimal
    {
        public void MakeSound()
        {
            Console.WriteLine("야옹");
        }
    }
    
    // 사용 예시
    class Program
    {
        static void Main(string[] args)
        {
            IAnimal animal1 = new Dog();
            IAnimal animal2 = new Cat();
    
            animal1.MakeSound(); // "멍멍" 출력
            animal2.MakeSound(); // "야옹" 출력
        }
    }

    위 예시에서 IAnimal 인터페이스는 MakeSound() 메서드를 정의합니다. Dog 클래스와 Cat 클래스는 IAnimal 인터페이스를 구현하여 MakeSound() 메서드를 각각 멍멍과 야옹으로 구현합니다. Main() 메서드에서는 IAnimal 인터페이스를 사용하여 각각의 클래스를 생성하고, MakeSound() 메서드를 호출합니다. 이를 통해 인터페이스를 사용하여 클래스 간의 상호 작용을 구현할 수 있음을 알 수 있습니다.


    추상 클래스 ( Abstract class )

     

    추상 클래스는 인스턴스를 만들지 않을 추상 메서드와 일반 메서드로 이루어져 있습니다. 추상 클래스는 일반 클래스와 마찬가지로 필드, 속성, 메서드 등을 포함할 수 있지만, 일반 메서드와 추상 메서드를 모두 가질 수 있습니다. 추상 클래스를 상속하는 클래스는 추상 메서드를 반드시 구현해야 합니다. 추상 클래스는 클래스의 기본 동작을 제공할 수 있으므로, 추상 클래스를 상속하는 클래스는 일부 메서드를 구현하지 않고도 작동할 수 있습니다.

     

    // Shape 추상 클래스 정의
    public abstract class Shape
    {
        public abstract double GetArea();
    }
    
    // Rectangle 클래스, Shape 추상 클래스 구현
    public class Rectangle : Shape
    {
        private double width;
        private double height;
    
        public Rectangle(double width, double height)
        {
            this.width = width;
            this.height = height;
        }
    
        public override double GetArea()
        {
            return width * height;
        }
    }
    
    // Circle 클래스, Shape 추상 클래스 구현
    public class Circle : Shape
    {
        private double radius;
    
        public Circle(double radius)
        {
            this.radius = radius;
        }
    
        public override double GetArea()
        {
            return Math.PI * radius * radius;
        }
    }
    
    // 사용 예시
    class Program
    {
        static void Main(string[] args)
        {
            Shape rectangle = new Rectangle(5, 10);
            Shape circle = new Circle(7);
    
            Console.WriteLine("Rectangle area: " + rectangle.GetArea()); // "Rectangle area: 50" 출력
            Console.WriteLine("Circle area: " + circle.GetArea()); // "Circle area: 153.93804002589985" 출력
        }
    }

    위 예시에서 Shape 추상 클래스는 GetArea() 추상 메서드를 정의합니다. Rectangle 클래스와 Circle 클래스는 Shape 추상 클래스를 상속하여 GetArea() 메서드를 면적을 계산하는 방식으로 각각 구현합니다. Main() 메서드에서는 Shape 추상 클래스를 사용하여 Rectangle과 Circle 객체를 생성하고, GetArea() 메서드를 호출합니다. 이를 통해 추상 클래스를 사용하여 클래스 간의 상속 구조를 구현할 수 있으며, 하위 클래스에서 필수적으로 구현해야 하는 메서드를 강제할 수 있음을 알 수 있습니다.


    따라서 인터페이스는 구현해야 할 동작의 목록을 정의하고, 클래스 간의 상호 작용을 정의할 때 사용됩니다. 추상 클래스는 클래스의 기본 동작을 제공하고, 추상 메서드를 정의하여 하위 클래스가 구현해야 하는 동작을 강제합니다.

     

    댓글

Designed by Tistory.