I have a class A -> class B (child of A) -> class C (child of B) and class A -> class D. How can I write a function that return objects from class B and class D, example below. How to implement the calling part?
The problem is If I do A a = getFunction() I cannot access the B's functions.
public <T> T getFunction(IntegrationType integrationType) {
return (T) integrationFactory.createIntegration(integrationType);
}
You can use the
instanceofoperator. For example:Or, if you are using Java 16 or higher, you can try the pattern matching as follows: