Hi, if we have a method on a abstract class, like...
# announcements
g
Hi, if we have a method on a abstract class, like this:
Copy code
abstract fun test() : String
can we, using somehow the new kotlin techniques to change the returning value of the function in the other abstract class which extend the first one, something like this?
Copy code
fun test(): String {
    return test().toString()
}

fun test(): Int {
}
The goal would be use to reuse the same name. Maybe something using the
by
keyword somehow? Composing the objects (where the problem here is that I have two abstract classes)
s
No, you’d be breaking the contract specified by the class you’re inheriting from
and I’m pretty sure there isn’t any sane way to overload a no-arg method
if your real
test
method can take different parameters than specified in the parent class then sure, there’s nothing stopping you. Otherwise, no, what you’ve described is not possible
d
You could use
generics
k
Generics don't help either I think?
👆 1
d
You are right. The thing is, it's not possible to have two functions in the same class with same signature. And the return type doesn't define the signature, meaning that both
test
functions have same signature,
g
It looks like we can if the second type inherits from the first one, like this:
a
Yea that's covariant return type It's legal because a subtype (should) honors the same contract as it's super type
💯 2
s
That (via the
covariant
keyword) is actually something that Dart has but Kotlin doesn’t https://dart.dev/guides/language/sound-problems#the-covariant-keyword