Avatar ·

Kotlin: Interface does not have constructors — how to create an instance?

Hi! I recently switched from Java to Kotlin and encountered a strange error. I have an interface `Drawable`: ```kotlin interface Drawable { fun draw() } ``` I want to create an instance of this interface using an anonymous class, as I did in Java: ```java // Java — works Drawable d = new Drawable() { @Override public void draw() { System.out.println("Drawing"); } }; ``` But in Kotlin I write: ```kotlin val d = Drawable() // Error! ``` And I get: "Interface Drawable does not have constructors". I tried different options — `Drawable { ... }`, `Drawable() { ... }` — nothing helps. How do I correctly create an anonymous implementation of an interface in Kotlin? Or do I have to create a separate class? I would appreciate a code example. <<>>

Problem: interfaces in Kotlin cannot be instantiated like classes

The error occurs because in Kotlin interfaces are not classes. In Java you can write new Interface