# Override getter force to override setter too

**URL:** <https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350>\
**Category:** Language Design\
**Created:** [March 3, 2017, 1:59pm UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350 "2017-03-03T13:59:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguelangellv](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miguelangellv/32/2432_2.png) [@miguelangellv](https://discuss.kotlinlang.org/u/miguelangellv)\
**Post date:** [March 3, 2017, 1:59pm UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/1 "2017-03-03T13:59:21Z")

</div>

I want override a getter of property

```auto
 class A {
   var foo : Bar = Bar()
}

class B : {
  override var foo : Bar 
     get() {
        thing();
       super.foo
      }
}

```

But this throw error: Or initialize foo in B or add setter.  
If initialize the compiler create a new variable foo in B (duplicate), so I need create a setter that only call to parent

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [March 4, 2017, 8:12am UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/2 "2017-03-04T08:12:12Z")

</div>

Yes, this is correct. We haven’t found this to be much of a problem in practice, but it’s possible that we’ll improve this later. Please feel free to watch/vote for [https://youtrack.jetbrains.com/issue/KT-12996](https://youtrack.jetbrains.com/issue/KT-12996)

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [March 7, 2017, 11:49am UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/3 "2017-03-07T11:49:03Z")

</div>

will this not solve the problem of initialization? does it still create duplicate variable?

```
override var foo: Bar = super.foo
        get() {
            initializeSomething()
            return super.foo
        }

```

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 7, 2017, 1:02pm UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/4 "2017-03-07T13:02:35Z")

</div>

> [@abuabdul](#):
>
> will this not solve the problem of initialization? does it still create duplicate variable?
> 
> override var foo: Bar = super.foo  
> get() {  
> initializeSomething()  
> return super.foo  
> }

If you initialise like this then, yes it will (initialising to super.foo is pointless). The initializer does not use the setter, and as the underlying field is always private overriding is not able to initialise it to a different value. If you want to set the value to a different one on subclass class instantiation use an init block to actually set it (keeping in mind that this will involve the setter).

```
override var foo:Bar
  get() {
    initializeSomething()
    return super.foo
  }

init { foo = newInitialValue }

```

---

<div class="post-metadata">

**Author:** ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)\
**Post date:** [March 7, 2017, 6:23pm UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/5 "2017-03-07T18:23:46Z")

</div>

I’d have thought that what @miguelangellv needs is this, which should compile and run OK and doesn’t need an initializer:

```kotlin
open class A {
    open var foo: Bar = Bar()
}

class B : A() {
    override var foo: Bar
        get() {
            thing()
            return super.foo
        }
        set(value) {              
            super.foo = value
        }
}

```

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [March 8, 2017, 6:49am UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/6 "2017-03-08T06:49:01Z")

</div>

Yes. But that is being tracked at [youtrack.com](http://youtrack.com) (KT-12996). I was trying to understand if the initialization like I did above could help him.

---

<div class="post-metadata">

**Author:** ![zsombor](https://avatars.discourse-cdn.com/v4/letter/z/4bbf92/32.png) [@zsombor](https://discuss.kotlinlang.org/u/zsombor)\
**Post date:** [February 2, 2018, 5:06pm UTC](https://discuss.kotlinlang.org/t/override-getter-force-to-override-setter-too/2350/7 "2018-02-02T17:06:30Z")

</div>

Since you say you haven’t found this much of a problem in practice I thought you might find my experience interesting.

I just ran into this after I migrated a largish Android project to Kotlin. There were a handful of overridden fields, some with only getters, some with only setters. Android Studio doesn’t give any indication (that I am aware of – maybe I missed something?) that I might need to override both getter and setter in order to call super, it just said “Property must be initialized.” So I did, and then I had a lot of fun hunting down the bugs resulting from the fact that the super’s getter or setter wasn’t called.

The fact that you have to create both getter and setter in order to call super is only a mild inconvenience, but the fact that you can seemingly side-step this requirement by adding initialization, AND that this introduces (from my perspective, anyway…) the side effect that the super getter/setter won’t be called is pretty surprising, IMHO.
