How to specialize a call to generic function with specific derrivation of that generic in Java?

generics, java, polymorphism, syntax

Solution

You can use the Double Dispatch pattern.

class Ideone
{
private static <T extends Base> void hassleSafeAdder(Container container, T value) {
        value.addToContainer(container);
    }

public static void main (String[] args) throws java.lang.Exception {
    Container container = new Container();

    hassleSafeAdder(container, new Child1());
    hassleSafeAdder(container, new Child2());
    hassleSafeAdder(container, new Child3());
    hassleSafeAdder(container, new Child4());

}

public static class Container {
    public void add(Child1 obj) { }
    public void add(Child2 obj) { }
    public void add(Child3 obj) { }
    public void add(Child4 obj) { }
}

public static abstract class Base { 

    public abstract void addToContainer(Container container);

}

public static class Child1 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child2 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child3 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}

public static class Child4 extends Base {

    public void addToContainer(Container container) {
        container.add(this);    
    }
}
}

Problem

I have a `Base` class and few of it's derived children, named `ChildN`. Also i have a `Container` class, with `ChildN`-specific methods. And finally i have a main class, where i wish to move all `Base`-generic logic into helper method, but somewhere in middle of this logic i may have need to put a `ChildN`-specific object into correct method of `Container` class. How do i do that? How to specialize a call to `hassleSafeAdder` with correct `ChildN` type, so it be accessible in this method and i could cast to it, or do some other thing which enable correct dispatching of object to right method? ``` // base class public static class Base { } // derived classes public static class Child1 extends Base { } public static class Child2 extends Base { } public static class Child3 extends Base { } public static class Child4 extends Base { } // class with derived types in method signature public static class Container { public void add(Child1 obj) { } public void add(Child2 obj) { } public void add(Child3 obj) { } public void add(Child4 obj) { } } // main class where execution happens public static class Main { private static <T extends Base> void hassleSafeAdder(Container container, T value) { // do some hassle container.add(value); // error, because T could be any Base derivation, while container has only ChildN-specific ones. // cleanup hassle resources } public static void main(String[] args) { Container container = new Container(); // how to parametrize this calls so they will be valid? hassleSafeAdder(container, new Child1()); hassleSafeAdder(container, new Child2()); hassleSafeAdder(container, new Child3()); hassleSafeAdder(container, new Child4()); } } ``` Desired pseudo-code: ``` hassleSafeAdder<Child1>(container, new Child1()); hassleSafeAdder<Child2>(container, new Child2()); hassleSafeAdder<Child3>(container, new Child3()); hassleSafeAdder<Child4>(container, new Child4()); ```

Original source