# Add smart cast of \`override var\` from interface \`var\`

**URL:** <https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358>\
**Category:** Language Design\
**Created:** [January 29, 2019, 7:04am UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358 "2019-01-29T07:04:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kolyall](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Kolyall](https://discuss.kotlinlang.org/u/Kolyall)\
**Post date:** [January 29, 2019, 7:04am UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/1 "2019-01-29T07:04:33Z")

</div>

Code:

```
    class AnyActivity : IAnyActivityView {
    override lateinit var myButton: MyButton
}

class AnyActivityPresenter {

    var view:IAnyActivityView = AnyActivity()

    fun makeFunc() {
        view.myButton.showProgress()
        view.myButton.hideProgress()
    }
}

class MyButton() : HasProgress {
    override fun showProgress() {
    }

    override fun hideProgress() {
    }
}

interface HasProgress {
    fun showProgress()
    fun hideProgress()
}

    interface IAnyActivityView {
        var myButton: HasProgress
    }

```

Why the error occurs  
Type of ‘`myButton`’ doesn’t match the type of the overridden var-property ‘`public abstract var myButton: HasProgress defined in IAnyActivityView`’ and `override lateinit var myButton: MyButton` doesn’t smart cast to `HasProgress`?

 ![223](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/c/c1c7aa9431f8acd7556d8f86b128da4536b139d3.png)

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [January 29, 2019, 7:53am UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/2 "2019-01-29T07:53:52Z")

</div>

> Why the error occurs  
> Type of ‘ `myButton` ’ doesn’t match the type of the overridden var-property ‘ `public abstract var myButton: HasProgress defined in IAnyActivityView` ’

Because it would override the property’s setter with a more specific type. While this is ok for getters, it is not allowed for setters. More generally: you cannot override any function with a more specific argument type. If you are coming from Java, it is the same there.

---

<div class="post-metadata">

**Author:** ![Kolyall](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Kolyall](https://discuss.kotlinlang.org/u/Kolyall)\
**Post date:** [January 29, 2019, 1:01pm UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/3 "2019-01-29T13:01:35Z")

</div>

> [@Kolyall](#):
>
> IAnyActivityView

Oh…Yes you are right. So can you tell me what should I write to get `myButton` as `HasProgress `, without creation any new method in `AnyActivity`?

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [January 29, 2019, 2:31pm UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/4 "2019-01-29T14:31:51Z")

</div>

Use `val` instead of `var`?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [January 29, 2019, 4:21pm UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/5 "2019-01-29T16:21:50Z")

</div>

Generics?

---

<div class="post-metadata">

**Author:** ![Kolyall](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Kolyall](https://discuss.kotlinlang.org/u/Kolyall)\
**Post date:** [January 30, 2019, 7:09am UTC](https://discuss.kotlinlang.org/t/add-smart-cast-of-override-var-from-interface-var/11358/6 "2019-01-30T07:09:13Z")

</div>

Resolved:  
`override lateinit var myButton: MyButton`

`val myButton: HasProgress`
