sveggen
sveggen

Reputation: 53

Implement interface and change parameter to specific object-type

I want to implement an interface and change the parameter in the create(any : Any) function to create(benefitModel : BenefitModel)in the BenefitStore-class which implements the interface.
This does not work and I get the following error message:
"Class 'BenefitStore' is not abstract and does not implement abstract member public abstract fun create(O: Any)...".

What is the best way for me to solve this problem?

StoreInterface.kt

interface StoreInterface {
    fun findAll(): List<Any>
    fun create(any : Any)
    fun update(any: Any)
    fun delete(any: Any)
}

BenefitStore.kt

class BenefitStore: StoreInterface, AnkoLogger {

override fun create(benefitModel: BenefitModel) {
...
}

Upvotes: 0

Views: 887

Answers (1)

Todd
Todd

Reputation: 31660

You can define StoreInterface to be generic.

I've also take the liberty of removing the redundant ...Interface on it, which is not idiomatic.

interface Store<T> {
    fun findAll(): List<T>
    fun create(arg : T)
    fun update(arg: T)
    fun delete(arg: T)
}

By using generics, you are telling Kotlin that T is a placeholder type that will be specified by the class implementing the interface:

class BenefitStore: Store<BenefitModel>, AnkoLogger {

    override fun create(arg: BenefitModel) {
        //...
    }
}

So in this case, Store will work with BenefitModels only. And all of the functions overridden in BenefitStore that came from Store, will require BenefitModel instances.

Upvotes: 1

Related Questions