I am exploring and actively using generics in production with Kotlin.
Kotlin + generics is a big puzzle for me, so maybe you can explain and help me understand how it works here, compared to Java.
I have class AbstracApiClient
(not really abstract)
class AbstracApiClient {
open protected fun makeRequest(requestBuilder: AbstractRequestBuilder) {
// ...
}
}
AbstractRequestBuilder
(not really abstract):
open class AbstractRequestBuilder {
...
}
ConcreteApiClient
that inherits AbstractApiClient
that should override makeRequest with ConcreteRequestBuilder
inherited from AbstractRequestBuilder
:
class ConcreteApiClient: AbstractApiClient() {
protected override fun makeRequest(requestBuilder: ConcreteRequestBuilder) {
// ...
}
}
class ConcreteRequestBuilder: AbstractRequestBuilder()
As I would have more concrete API clients. I would like to make an abstraction that I can pass inherited concrete requests builders and override `make requests method.
- I tried using it as it is but wouldn't work
- I tried this notation
protected open fun <R: ApiRequestBuilder> make request(request builder: R)
but it won't match overriding function which I want it to be:protected override fun make request(request builder: ConcreteRequestBuilder)
What other options do I have? Am I missing something here?
Note: I cannot use interface
or abstract classes
in this scenario, so ideally I would like to find a way with inheritance and functions overriding.