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?
In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.
In kotlin, we can achieve this by using the “Object” keyword; the object declaration contains properties, methods, and so on. However, mainly it does not allowed the constructor to create the object. Like that object, a keyword is used for to create the objects of the anonymous class known as the anonymous object.
Anonymous classes are inner classes with no name. Since they have no name, we can't use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface.
In Kotlin, we cannot create an instance of an abstract class. Abstract class could only be inherited by a class or another Abstract class. So, to use abstract class, create another class that inherits the Abstract class.
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.
When a class is defined in Kotlin with an abstract keyword, then it is known as an abstract class. In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature.
To create an instance of a class, call the constructor as if it were a regular function: Copied! Kotlin does not have a new keyword. The process of creating instances of nested, inner, and anonymous inner classes is described in Nested classes. Classes can be derived from each other and form inheritance hierarchies.
In Kotlin we can override the non-abstract open member function of the open class using the override keyword followed by an abstract in the abstract class. In the below program we will do it. An abstract member of an abstract class can be overridden in all the derived classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With