0
0
KotlinComparisonBeginner · 4 min read

Abstract Class vs Interface in Kotlin: Key Differences and Usage

In Kotlin, a abstract class can hold state and provide constructor logic, while an interface cannot hold state but can declare abstract methods and default implementations. Use abstract classes when you need shared code and state, and interfaces for defining contracts that multiple classes can implement.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of abstract classes and interfaces in Kotlin.

FeatureAbstract ClassInterface
State (fields)Can have state (properties with backing fields)Cannot have state (properties are abstract or have default getters)
ConstructorsCan have constructorsCannot have constructors
Multiple inheritanceSupports single inheritance onlySupports multiple inheritance (a class can implement many interfaces)
Method implementationCan have both abstract and concrete methodsCan have abstract methods and default implementations
Use caseUse for shared base functionality and stateUse for defining contracts and capabilities
Access modifiersCan have visibility modifiers (private, protected, etc.)Members can have visibility modifiers (default is public)
⚖️

Key Differences

An abstract class in Kotlin is like a blueprint for other classes that can hold state and provide constructor logic. It can have properties with backing fields, meaning it can store data. It also supports visibility modifiers like private or protected, allowing you to control access to its members. However, a class can only inherit from one abstract class, limiting multiple inheritance.

On the other hand, an interface defines a contract that classes can implement. Interfaces cannot hold state but can declare properties without backing fields or provide default implementations for methods. They do not have constructors, and members can have visibility modifiers (default is public). Kotlin allows a class to implement multiple interfaces, enabling flexible multiple inheritance of behavior.

In summary, use abstract classes when you want to share code and state among related classes, and use interfaces when you want to define capabilities or contracts that can be applied across unrelated classes.

⚖️

Code Comparison

kotlin
abstract class Animal(val name: String) {
    abstract fun sound(): String

    fun describe() = "This is $name"
}

class Dog(name: String) : Animal(name) {
    override fun sound() = "Woof"
}

fun main() {
    val dog = Dog("Buddy")
    println(dog.describe())
    println(dog.sound())
}
Output
This is Buddy Woof
↔️

Interface Equivalent

kotlin
interface Animal {
    val name: String
    fun sound(): String
    fun describe() = "This is $name"
}

class Dog(override val name: String) : Animal {
    override fun sound() = "Woof"
}

fun main() {
    val dog = Dog("Buddy")
    println(dog.describe())
    println(dog.sound())
}
Output
This is Buddy Woof
🎯

When to Use Which

Choose an abstract class when:

  • You need to share common code and state (properties with backing fields) among related classes.
  • You want to provide constructor logic or control member visibility.
  • Your design fits a clear class hierarchy with single inheritance.

Choose an interface when:

  • You want to define a contract or capability that can be added to any class, unrelated or related.
  • You need multiple inheritance of behavior since a class can implement many interfaces.
  • You do not need to store state or provide constructor logic.

Key Takeaways

Abstract classes can hold state and have constructors; interfaces cannot.
A class can inherit only one abstract class but can implement multiple interfaces.
Use abstract classes for shared base functionality and interfaces for flexible contracts.
Interfaces can provide default method implementations but cannot store data.
Choose based on whether you need state and inheritance (abstract class) or just behavior contracts (interface).