# Why cannot you override property defined as var with inherited property class?

**URL:** <https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518>\
**Category:** Support\
**Created:** [February 10, 2019, 7:20pm UTC](https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518 "2019-02-10T19:20:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtolstyi](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@dtolstyi](https://discuss.kotlinlang.org/u/dtolstyi)\
**Post date:** [February 10, 2019, 7:20pm UTC](https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518/1 "2019-02-10T19:20:28Z")

</div>

Hello,

I know that title is hard to understand, so here’s an example of the issue:

```auto
open class AbstractB(val param1: String)
class B(param1: String) : AbstractB(param1)

open class AbstractA(open var param: AbstractB)
class A(override var param: B) : AbstractA(param)

```

So, IntelliJ IDEA says that `Var-property type is 'B', which is not a type of overridden`  
But, if we change `class AbstractA` param to `val` the issue doesn’t appear:

```auto
open class AbstractB(val param1: String)
class B(param1: String) : AbstractB(param1)

open class AbstractA(open val param: AbstractB)
class A(override val param: B) : AbstractA(param)

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![SOF3](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sof3/32/5646_2.png) [@SOF3](https://discuss.kotlinlang.org/u/SOF3)\
**Post date:** [February 10, 2019, 7:45pm UTC](https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518/2 "2019-02-10T19:45:01Z")

</div>

This results in covariant argument type and leads to type errors. See [Covariance and contravariance (computer science) - Wikipedia](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Contravariant_method_argument_type)

See this for example:

```kotlin
class B2 : AbstractB("2")
val b1 = B("1")
val b2 = B2()
val a: AbstractA = A(b1) // b2 is incompatible with A() constructor because it requires B not AbstractB
a.param = b2 // b2 is compatible, since a.param is AbstractA.param which is AbstractB

```

However, `b2` is passed to the underlying `A.param`, which does not accept `B2` (`B2` extends `AbstractB` but not `B`). This leads to type errors.

Therefore, `var` overrides must always use the same type, while `val` overrides may use a more specific type.  
This is because `var` generates both a getter (return type) and a setter (argment type), while `val` only generates a getter (return value).

---

<div class="post-metadata">

**Author:** ![pa7z2kd](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@pa7z2kd](https://discuss.kotlinlang.org/u/pa7z2kd)\
**Post date:** [February 10, 2019, 7:45pm UTC](https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518/3 "2019-02-10T19:45:54Z")

</div>

```
val a: AbstractA = A()
a.param = AbstractB() // error: a's real type is A, and its param now has a type of AbstractB, which is not B

```

---

<div class="post-metadata">

**Author:** ![dtolstyi](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@dtolstyi](https://discuss.kotlinlang.org/u/dtolstyi)\
**Post date:** [February 10, 2019, 8:00pm UTC](https://discuss.kotlinlang.org/t/why-cannot-you-override-property-defined-as-var-with-inherited-property-class/11518/4 "2019-02-10T20:00:08Z")

</div>

I see… The thing with `setters` makes sense. Now it’s clear. Thank you!
