Issue
It seems that both are working. But should I annotate @Async in interface or implementation class?
public interface Test {
@Async
void A()
void B();
}
public class TestImpl implements Test {
@Override
void A(){}
@Override
@Async
void B(){}
}
Solution
To flesh out your example --
public interface Test{
@Async
void A()
void B();
}
public class TestImpl implements Test {
@Override
void A(){}
@Override
@Async
void B(){}
}
public class AnotherTestImpl implements Test {
@Override
void A(){}
@Override
void B(){}
}
For both TestImpl
and AnotherTestImpl
, the A()
method is async -- that's inherited from the interface.
On the other hand, B()
is not async in the interface. TestImpl
declares its implementation as async, so it's async there. But AnotherTestImpl
does not, so it's also not async there.
| Method | Interface | TestImpl | AnotherTestImpl |
|--------|-----------|----------|-----------------|
| A() | async | async | async |
| B() | not async | async | not async |
Basically, if you declare it on the interface method, it will be inherited by all implementors.
Answered By - Roddy of the Frozen Peas
Answer Checked By - Pedro (JavaFixing Volunteer)