Why is AtomicInteger abstract in Kotlin? (it works fine in Java)

I tried to do something similar to this (in fact you do not need to read the link to understand this question, this is just for the link), and I am writing this:

class CallArbiter: AtomicInteger { // error constructor(initialValue: Int) : super(initialValue) constructor() : super() } 

The compiler says:

Error: (8, 1) Kotlin: Class 'CallArbiter' must be declared abstract or implement an abstract member of the base class public abstract fun toByte (): byte defined in java.util.concurrent.atomic.AtomicInteger

I cannot understand why it requires me to implement these methods. I have not seen them in the AtomicInteger class. Everything is fine in Java.

+5
source share
1 answer

AtomicInteger extends java.lang.Number , but in Kotlin this type is mapped to kotlin.Number .

In kotlin.Number these abstract methods are defined (which you can see in the API ):

toByte , toInt , toChar , etc.

If you are debugging this line of code: AtomicInteger(2).toByte() you can see that the java.lang.Number::byteValue method is used, this is done using specific compiler methods.

+4
source

Source: https://habr.com/ru/post/1271255/


All Articles