// Define two interfaces with different functionalities
interface Walker {
fun walk()
}
interface Swimmer {
fun swim()
}
// Implement both interfaces in a single class
class Duck : Walker, Swimmer {
override fun walk() {
println("Duck is walking")
}
override fun swim() {
println("Duck is swimming")
}
}
fun main() {
val duck = Duck()
duck.walk() // Outputs: Duck is walking
duck.swim() // Outputs: Duck is swimming
}
kotlinCopy code
interface Flyer {
fun fly()
}
interface Diver {
fun dive()
}
class Seagull : Flyer, Diver {
override fun fly() {
println("Seagull is flying")
}
override fun dive() {
println("Seagull is diving")
}
}
fun testAbilities(bird: Any) {
when (bird) {
is Flyer -> bird.fly()
is Diver -> bird.dive()
}
}
fun main() {
val seagull = Seagull()
testAbilities(seagull) // Outputs: Seagull is flying and then Seagull is diving
}
kotlinCopy code
interface Vegetarian {
fun eatPlants()
}
interface Carnivore {
fun eatMeat()
}
class Bear : Vegetarian, Carnivore {
override fun eatPlants() {
println("Bear eats plants")
}
override fun eatMeat() {
println("Bear eats meat")
}
}
fun main() {
val bear = Bear()
bear.eatPlants() // Outputs: Bear eats plants
bear.eatMeat() // Outputs: Bear eats meat
}