0

I think this has been asked before but I'm not sure why it isn't working

I have a couple of interfaces

interface SBoard {
    val size: Int
}

interface GBoard<T> : SquareBoard {
}

with classes

open class SBoardImpl (override val size: Int) : SBoard {
    lateinit var allCells: Array<Array<CellImpl>>

    init {
        println ("Parent Generator: " + this.size.toString())
    }

   ...
}

class GBoardImpl<T> (override val size: Int): SBoardImpl (size), GBoard<T>{
    lateinit var cellValues: Array<Array<Any?>>

    init {
        println ("Child Initialisation:" + super.size.toString())
    }

    ...
}

when I create GBoardImpl like this

GameBoardImpl<T>(2)

e.g.

val g = createGBoard<Char>(2)

The size doesn't get passed to the super (SBoard) ? I get output of

Parent Generator: 0

I've ripped quite a bit out so I hope it still makes sense but essentially the SBoard init does get called (i.e. the parent) and the value of size is 0 instead of 2 (in this example). All the examples I've seen (or at least the way I've read them) show that the inclusion of the parameter in the method signature should pass it up

i.e.

class GBoardImpl<T> (override val size: Int): SBoardImpl (size)... 

means that size gets passed to the SBoardImpl/Parent initialiser. Is that a misunderstanding or can anyone tell me what I've done wrong ?

gringogordo
  • 1,990
  • 6
  • 24
  • 60
  • The simple answer is: you should remove `override val` from the GBoardImpl constructor. The full answer was given here in another question, but I can't seem to find it. – yole Nov 14 '18 at 16:37
  • I answered something similar recently: https://stackoverflow.com/questions/53179841/beginner-question-inheritance-why-isnt-my-age-constructor-parameter-used/53185727#53185727. There are other similar answers too, I'm sure. – Yoni Gibbs Nov 14 '18 at 17:07
  • Thanks must have done inept searching. Agree it's a dupe – gringogordo Nov 14 '18 at 18:29

0 Answers0