https://kotlinlang.org logo
Title
w

wei

08/29/2018, 2:37 AM
I have an abstract class
A
that has an abstract member property defined by an interface:
interface Tool {
  fun run(command: String): Boolean
}

abstract class A {
  abstract protected var tool: Tool
  fun execute(command: String) : Boolean {
    return tool.run(command)
  }
}
Then I have another class
B
that inherits from
A
and uses guice to inject the
tool
member property at startup.
class B {
  @Inject
  @Transient
  override protected var tool: Knife
}
where
Knife
is a concrete class implementing the interface
Tool
. Intellij is complaining to me:
Var property tye is `Knife`, which is not a type of overridden protected abstract var tool: Tool defined in com.myorg....
.
a

Andreas Sinz

08/29/2018, 7:37 AM
@wei The problem is
var
combined with using a subtype of
Tool
, e.g. the following example fails:
val b = B()
val a: A = b

a.tool = Drill() //Fails at runtime, because B.tool must be a Knife, but Drill is used
You can use guice contructor injection and use a
val
or set the type of
B.tool
to
Tool