D
D
Dred Wolf2021-03-21 21:42:35
Kotlin
Dred Wolf, 2021-03-21 21:42:35

How to make a "static" overridden function inherited from interfaces?

The situation is this. There are 2 interfaces that have the same function, with the same implementation. Therefore, the class needs to implement this function

interface Animal{
    fun voice(){
        println("звуки природы......")
    }
}

interface Cat {
    fun voice(){
        println("мяу......")
    }
}

// здесь мне бы хотелось, чтобы класс Tiger использовал реализацию из интрфейса Cat
// НО ТАКЖЕ МНЕ ХОТЕЛОСЬ БЫ ИСПОЛЬЗОВАТЬ ЭТУ ФУНКЦИЮ БЕЗ СОЗДАНИЯ ОБЪЕКТА
class Tiger : Animal, Cat {
    companion object {
        override fun voice() {
            super<Cat>.voice()
        }
    }
}


error: Not an immediate supertype

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-03-22
@DredWulf

I don't know why you want this, it's strange, in my opinion. But you can make it work, just make the companion object implement this interface:

class Tiger {
    companion object : Animal, Cat {
        override fun voice() {
            super<Cat>.voice()
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question