Search code examples
javakotlinclassmethodsinterface

How interface::class.java works for umimplemented methods in that interface


fun main(){
    val bal = Host::class.java
}

interface Host{
    fun void()
}

I have created simple code here, One Interface that was instantiated here with a class like Host::class.java. I see bal take the type as Class. Its creating a class from interface thats ok but how it can create class from interface without the methods that need to be implemented as a contract between interface. Also when i call bal. i couldn't find void() method from bal instance.

What is the reason it differs from normal implementation of interface in a class? I'm really for any kind of answers :)


Solution

  • bal is an instance of Class<Host>, not an instance of Host. Class is a class that contains information about the definition of a class or interface, and does not represent an instance of that class or interface. It is typically used for reflection. No class implementation of your interface has been created in your code.