- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create an instance of an anonymous interface in Kotlin?
Kotlin has been developed over JVM, hence it supports most of the features of JVM. Java provides a feature called anonymous inner classes to handle the cases where we have to create an object of a class with a slight modification, without declaring a new subclass. An anonymous inner class doesn't have a name; we define it directly at the instantiation line.
However, Kotlin uses object expressions to provide the same sub-class functionality. In Kotlin, we can create an object expression of an interface by implementing its abstract methods. This implementation technique is known as anonymous interface.
Example – Anonymous Interface in Kotlin
The following example demonstrates how we can implement an anonymous interface in Kotlin.
fun interface myInterface<T> { fun call(context: T) } fun main() { val a = myInterface<String> { println("This is implementation of $it") } a.call("myInterface") }
Output
On execution, it will produce the following output −
This is implementation of myInterface
- Related Articles
- How to create an instance of an abstract class in Kotlin?
- How to implement an interface using an anonymous inner class in Java?
- How to create an empty array in Kotlin?
- Golang Program to Create an Interface
- How can we create an instance of VarHandle in Java 9?
- How to implement interface in anonymous class in C#?
- Can we create an object for an interface in java?
- How to create Tab Layout in an Android App using Kotlin?
- How to create GridView Layout in an Android App using Kotlin?
- How to create a WebView in an Android App using Kotlin?
- How to create Text to Speech in an Android App using Kotlin?
- Create new instance of an Array with Java Reflection Method
- How to create custom Alert Dialogs in an Android App using Kotlin?
- How to check if an object is an instance of a Class in JavaScript?
- How to add an item to an ArrayList in Kotlin?

Advertisements