I have the following code:
public abstract class AbstractClass<T> {
final A a;
@Inject
AbstractClass(A a) {
this.a = a;
}
}
public class B extends AbstractClass<C> {
final D d;
@Inject
B(D d) {
super(); // this fails
this.d = d;
}
}
My class B
extends AbstractClass
and AbstractClass
uses @Inject
to inject A
into it. In class B
I cannot call super()
because AbstractClass
has an argument in the constructor. How can I handle the dependency injection of the superclass in the subclass to get super()
working?
How can I construct a class when the superclass uses @Inject
?
You need to accept an A
as well:
@Inject
B(A a, D d) {
super(a);
this.d = d;
}
Then Guice should inject both the A
and the D
, and you just pass the A
up to the superclass constructor. Just because a constructor is marked with @Inject
doesn't mean it can only be invoked by Guice... In fact, I would actually remove the @Inject
from AbstractClass
- unless Guice has some magic I'm unaware of, it's not going to be able to inject that anyway.