[kotlin] 코틀린에서 추상 클래스와 인터페이스의 특징 비교

코틀린은 자바와 같이 객체 지향 프로그래밍을 지원하며, 추상 클래스와 인터페이스를 정의할 수 있습니다. 이 두 가지 기능은 다양한 상황에서 사용될 수 있으며, 각각의 특징을 이해하는 것이 중요합니다.

추상 클래스 (Abstract Class)

abstract class Animal {
    abstract fun makeSound()
    
    fun sleep() {
        println("동물이 잠자고 있습니다.")
    }
}

class Dog : Animal() {
    override fun makeSound() {
        println("멍멍!")
    }
}

인터페이스 (Interface)

interface Animal {
    fun makeSound()
}

interface CanFly {
    fun fly()
}

class Bird : Animal, CanFly {
    override fun makeSound() {
        println("짹짹!")
    }
    
    override fun fly() {
        println("날아갑니다.")
    }
}

추상 클래스와 인터페이스의 비교

참고 자료