Question

Consider the following program:

import java.util.List;
import java.util.ArrayList;

public class TypeTest {

    public static class TypeTestA extends TypeTest {

    }

    public static class TypeTestB extends TypeTest {

    }

    public static final class Printer {
        public void print(TypeTest t) {
            System.out.println("T");
        }

        public void print(TypeTestA t) {
            System.out.println("A");
        }

        public void print(TypeTestB t) {
            System.out.println("B");
        }

        public <T extends TypeTest> void print(List<T> t) {
            for (T tt : t) {
                print(normalize(tt.getClass(), tt));
            }
        }

        private static <T> T normalize(Class<T> clz, Object o) {
            return clz.cast(o);
        }

    }
    public static void main(String[] args) {
        Printer printer = new Printer();
        TypeTest t1 = new TypeTest();
        printer.print(t1);
        TypeTestA t2 = new TypeTestA();
        printer.print(t2);
        TypeTestB t3 = new TypeTestB();
        printer.print(t3);
        System.out.println("....................");
        List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
        tb1.add(t3);
        printer.print(tb1);
    }
}

The main method now prints:

T  
A  
B  
....................  
T  

What should I do to make it print the followings?

T  
A  
B  
....................  
B  

I'd like to avoid writing a loop such as the following for each of the type that can be printed:

   public void printTypeTestB(List<TypeTestB> t) {
        for (TypeTestB tt : t) {
            print(tt);
        }
    }
Was it helpful?

Solution

The root of your problem is that Java method overloads are resolved at compile time based on the declared type of the method argument expressions. Your program seems to be trying to use runtime dispatching to different method overloads. That simply doesn't work in Java.

The fact that you are using generics in your example is a bit of a red herring. You would have the same problem if you replaced the type parameter <T> with TypeTest.

OTHER TIPS

Concider creating a visitor interface which knows about all relevant subtypes.

public class TypeTestFoo {

interface TypeTestVisitor {
    void visit(TypeTestA t);
    void visit(TypeTestB t);
    void visit(TypeTest t);
}

interface TypeTest {
    void accept(TypeTestVisitor visitor);
}

public static class TypeTestA implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static class TypeTestB implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static final class Printer implements TypeTestVisitor {
    public void visit(TypeTestA t) {
        System.out.println("A");
    }

    public void visit(TypeTestB t) {
        System.out.println("B");
    }

    public void visit(TypeTest t) {
        System.out.println("T");
    }

}
public static void main(String[] args) {
    Printer printer = new Printer();
    TypeTest t1 = new TypeTest() {
        public void accept(TypeTestVisitor visitor) {
            visitor.visit(this);
    }};
    t1.accept(printer);    
    TypeTestA t2 = new TypeTestA();
    t2.accept(printer);
    TypeTestB t3 = new TypeTestB();
    t3.accept(printer);
    System.out.println("....................");
    List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
    tb1.add(t3);
    for (TypeTestB each : tb1) {
        each.accept(printer);
    }
}

}

This should print out what you wanted:

T
A
B
....................
B

The types are listed in the interface which allows for compile-time overloading. On the other hand, this is a single point where you have put the subtypes for which you wan't to parameterize behavior. Java is not a very dynamic language... :)

candidate for the convoluted "visitor pattern".

or simply move print() method from Printer to TypeTest

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top