Sobes.tech
Junior — Middle

What is the role of interfaces in software development and why are they used?

sobes.tech AI

Answer from AI

Interfaces in software development serve to define a contract — a set of methods that a class or object must implement. They allow separating the description of behavior from the specific implementation, which increases flexibility and extensibility of the code.

Interfaces are used for:

  • Ensuring polymorphism — different classes can implement the same interface in different ways.
  • Simplifying testing — implementations can be replaced with stubs or mocks.
  • Improving support and scalability — new implementations can be added easily without changing existing code.

Example in Kotlin (Android):

interface ClickListener {
    fun onClick()
}

class Button(private val listener: ClickListener) {
    fun click() {
        listener.onClick()
    }
}

class MyClickListener : ClickListener {
    override fun onClick() {
        println("Button clicked")
    }
}

val button = Button(MyClickListener())
button.click()  // Will output: Button clicked