I want to use the polymorphism in Java in way to handle the following scenario:
public class Main {
public static void main (String[] args) {
handle(new B());
handle(new C());
}
public static void handle(A a){
// here I want to create a F<T>, in way that:
// * F must be C or D if A is B or C
// * T must B the real type of A
// e.e:
// new F<A>();
// task => see the output from D or E
}
}
class A {
}
class B extends A {
}
class C extends A {
}
class D<B> extends F{
public D(){
System.out.println("Created D of B");
}
}
class E<C> extends F{
public E(){
System.out.println("Created E of C");
}
}
abstract class F<T>{
}
The entry point is the handle
method of the class Main
.
The method receive an object A
, that can be an instance of the class B
or C
.
My task is to find a way to create a new Object F
that depends on the real Type of the A
instance received, and the F
object must be C
or D
depending on A
, if it's B
or C
respectively.
Any idea will be appreciated.
Thanks.