I want to write method that would accept parameter of types a.A or b.B. Currently it's implemented:
import a.A;
import b.B;
...
public void doSth(A arg) {
SpecificClass.specificMethod(arg);
}
public void doSth(B arg) {
SpecificClass.specificMethod(arg);
}
I want to have one generic method "doSth" that use wildcards and accepts only a.A or b.B. Important information a.A and b.B aren't subtypes of each other. The only common type is java.lang.Object.
Any help?
Assuming you could do that, if A and B have no common superclass, you won't be able to call any methods on the arguments but Object's methods.
So I think the only 2 reasonable solutions are:
You may wrap both A and B extending a common interface, just like:
Then modify your method doSth to accept a CommonWrapper object as parameter:
will be called :
But it doesn't restrict to anything that can extend object, whats the point everything does.I would suggest having both your classes implement a common interface and then program to that interface.