You can't.
The companion object is a rough equivalent of the static keyword in Java.
The doStuff()
function of class A
(and its subclasses) can only be called from an actual object of that class (like A().doStuff()
or B().doStuff()
)
When trying to call that function from B
's companion object, there is no such object of A
(or B
) on which you could call that function, since you're in a static context.
If you'd write the Java equivalent of what you posted, you'd receive the error
non-static method cannot be referenced from a static context
which is more descriptive than what you're probably getting from Kotlin's compiler and is well explained here.