160

Assume that KeyAdapter is an abstract class with several methods that can be overridden.

In java I can do:

KeyListener keyListener = new KeyAdapter() {
    @Override public void keyPressed(KeyEvent keyEvent) {
        // ...
    }
};

How to do the same in Kotlin?

Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
Tvaroh
  • 6,645
  • 4
  • 51
  • 55
  • 1
    I didn't downvote, but your question shows very little research effort. A simple Google search for _anonymous classes in kotlin_ turns up [this page](http://blog.jetbrains.com/kotlin/2013/02/kotlin-m5-1/) which discusses--of all things--how to create the Kotlin analogue to an anonymous inner class. – Ted Hopp Jul 07 '13 at 22:20
  • 5
    I'm also just starting with Kotlin. This question comes up as the first hit with a very reasonable google search. What was very confusing for me was that the KeyAdapter in the question is most likely the java.awt.event.KeyAdapter, which is an abstract class with 3 methods and the simple `val k: KeyAdapter = KeyAdapter { println("Hello, world!") }` wont work. (However I was looking for the lambda syntax.) I think I'll look for anoter question with this exact title, but a better formulated question and link this one to it with a comment... – Peter Lamberg Jun 07 '16 at 06:22
  • @PeterLamberg it's not java.awt it's Intellij plugins API. – Tvaroh Jun 07 '16 at 06:34
  • This question comes up as a first hit in some google searches for which the [the relevant question is closer to this so I'm linkin it here](http://stackoverflow.com/questions/37672023/how-to-create-an-instance-of-anonymous-interface-in-kotlin/37672036#37672036) – Peter Lamberg Jun 07 '16 at 06:36
  • @Tvaroh Could you add the definition of the interface/class (possibly in shortened form) to the beginning of your question? – Peter Lamberg Jun 07 '16 at 06:39
  • @PeterLamberg I have not that code anymore, I think it's deadly clear anyway. – Tvaroh Jun 07 '16 at 08:01
  • See also similar question about anonymous instance of interface: http://stackoverflow.com/questions/33552299/how-to-create-an-anonymous-implementaiton-of-an-interface – Vadzim Oct 24 '16 at 13:52
  • @brian-roach I asked this question today at our team premises. My polite co-workers explained to me that I must be stupid for asking this as it "demonstrate a minimal understanding of the problem being solved". Thanks – mkorpela Jan 17 '17 at 11:09
  • 14
    I don't see why this is flagged as too simple. The question is upvoted a lot, meaning a lot of people face this problem. Because of the language differences, it's not obvious whether the first hit on Google is the solution. I skimmed over the official documentation but this question on SO cleared it up. – Muz Jul 14 '17 at 03:50

2 Answers2

241

From the official Kotlin language documentation:

window.addMouseListener(object : MouseAdapter() { 
    override fun mouseClicked(e : MouseEvent) { 
    // ... 
}

Applied to your problem at hand:

val keyListener = object : KeyAdapter() { 
    override fun keyPressed(keyEvent : KeyEvent) { 
    // ... 
} 

As Peter Lamberg has pointed out - if the anonymous class is actually an implementation of a functional interface (i.e. not of an abstract class), SAM Conversions can be used to simplify this statement even further:

val keyListener = KeyAdapter { keyEvent ->
    // ...
}

Please also note this discussion about the different usage of interfaces defined in Java and Kotlin.

thedayturns
  • 9,723
  • 5
  • 33
  • 41
Michael Lang
  • 3,902
  • 1
  • 23
  • 37
9

In Java

private Comparator<Integer> comparator = new Comparator<Integer>(){
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
    }
};

in Kotlin

private val comparator = object: Comparator<Int> {
    override fun compare(o1: Int, o2: Int): Int {
        return o1.compareTo(o2)
    }
}

But because Comparator is a FunctionalInterface we can take advantage of SAM conversions and write

private val comparator = Comparator<Int> { o1, o2 -> o1.compareTo(o2) }

Please note that SAM feature only works for Functional interfaces defined in java, if you need to create object of a kotlin interface with single method then you will have to use object syntax.

mightyWOZ
  • 7,946
  • 3
  • 29
  • 46
  • I like this answer too as the syntax is easier to "get into". Although I aspire to use the other answer's more simple syntax, this one is what I would use at the beginning and did use during TDD.Academy livestream, Episode 36, Android: Text Boxes and TDD #7: https://www.youtube.com/watch?v=ikQAmBNdRRo – Lance Kind Aug 29 '21 at 13:13