-1

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.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82
  • 2
    Do you understand that at execution time, there's no such thing as a `F` or `F`? There's just an `F`. Picking whether you want to create a `D` or an `E` makes more sense, but your declarations are messed up - did you mean `class D extends F`? (Currently, `D` is a generic type declaring a new type parameter called `B`.) – Jon Skeet Sep 09 '16 at 10:08
  • you can check if A is an instance of B or C using : "if (A instanceof B) //do something " and the same to check if A is an instance of C. The instance of is a java keyword – Krzysztof Cichocki Sep 09 '16 at 10:23
  • Add a abstract method to `A` which returns `F`. `abstract F create();`. In `B` and `C` you implement them, where you know which class to create. This is a basic example of polymorphism. – Felix S. Sep 09 '16 at 10:45

3 Answers3

1

@See Java type-erasure concepts. doc

One of the things you can do is to introduce virtual method in A:

F<? extends A> supply();

So, with the help of polymorphism you will delegate instantiation to the concrete class. This approach is similar to Template method pattern.

Sergei Rybalkin
  • 3,337
  • 1
  • 14
  • 27
0

you can check if a is an instance of B or C using instanceof keyword, this is probably the handle method implementation your are looking for (more or less):

 public static F handle(A a){

        if (a instanceof B) {
           // a is B, do something about that, possibly create some D<B> ?
           return new D();
        }

        if (a instanceof C) {
           // a is C, do something about that, possibly  create some E<C> ?
           return new E();
        }

        // if somehow you get here, that means something weird happened
        throw new RuntimeException("unsupported case");

    }
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

I have solved with this implementation:

public class Main {

    private static D d;

    public static void main (String[] args) {
        handle(new B());
        handle(new C());
    }

    public static void handle(A a){     
        d = solveD(a);
        d.handle(a);
    }

    private static D solveD(A a){
        if (a instanceof B){
            return new E();
        } else return new F();
    }

}   

class A {

}

class B extends A {

}

class C extends A {

}

interface D<T extends A>{
    public void handle(T t);
}

class E implements D<B> {

    @Override
    public void handle(B b){
        System.out.println("Handling E of B");
    }
}

class F implements D<C>{

    @Override
    public void handle(C c){
        System.out.println("Handling F of C");
    }

}

Using Spring, it's not necessary the method solveD, because we can get the bean based on the a.getClass().getSimpleName(), annotating E and F with @Component.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82