Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an instance of anonymous class of abstract class in Kotlin?

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?

like image 863
Tvaroh Avatar asked Sep 27 '22 18:09

Tvaroh


People also ask

How do I create an instance of abstract class 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.

How do you make an anonymous class on Kotlin?

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.

Can we create instance of anonymous class?

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.

Can not create instance of abstract class Kotlin?

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.

What is an anonymous class in Kotlin?

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.

What is an abstract class in Kotlin?

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.

How do I create an instance of a class in Kotlin?

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.

How to override non-abstract functions in Kotlin?

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.


1 Answers

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.

like image 231
Michael Lang Avatar answered Oct 20 '22 16:10

Michael Lang