Interfaces in Kotlin - Object Oriented Programming(OOP)

Опубликовано: 30 Март 2026
на канале: Programming with Alex
45
2

Kotlin and Android Full Course: https://www.udemy.com/course/kotlin-m...

What are interfaces?

In Kotlin, an interface is a blueprint for a class. It defines a set of abstract methods and properties that must be implemented by any class that implements the interface. Interfaces in Kotlin can also contain default method implementations, properties, and companion objects.

Here's a simple example of an interface:

```kotlin
interface Shape {
fun area(): Double
fun perimeter(): Double
}
```

In this example, the `Shape` interface declares two abstract methods, `area()` and `perimeter()`. Any class that implements the `Shape` interface must provide concrete implementations for these methods.

A class can implement one or more interfaces using the `: InterfaceName` syntax:

```kotlin
class Circle(radius: Double) : Shape {
private val radius: Double = radius

override fun area(): Double {
return Math.PI * radius * radius
}

override fun perimeter(): Double {
return 2 * Math.PI * radius
}
}
```

Here, `Circle` implements the `Shape` interface and provides specific implementations for the `area()` and `perimeter()` methods.