Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an abstract getter with property

Tags:

kotlin

I have java code:

public abstract class A {
   abstract int getA()
}

I tried:

class B : A() {
    val a = 0
}

Doesn't compile.

class B : A() {
    override val a = 0
}

Still doesn't compile.

class B : A() {
    override val a: Int get () = 1
}

Still doesn't compile.

class B : A() {
    override val a: Int override get () = 1
}

Still doesn't compile.

class B : A() {
    val a: Int override get () = 1
}

None of them are working. Does that mean I can only use

class B : A() {
    override fun getA() = 1
}

? I think the last one(overriding the method) is ugly.

This could be worse when you have a getter-setter pair. It's expected to override getter-setter pair with a var property, but you have to write two methods.

like image 210
ice1000 Avatar asked Jun 11 '17 08:06

ice1000


1 Answers

According to @Miha_x64 ,

functions can be overriden only with a function.

Seems that I was trying something impossible.

like image 64
ice1000 Avatar answered Nov 03 '22 09:11

ice1000