title: Java dynamic downcasting from generic list
body: <p>how i can dynamic downcast objects, with out instanceof statement?
  I reading Bruce Eckel's Thinking in Java, and there using Class, and there is such a theme, but I was not approached
  P.s. Sorry for my English.</p>
  
  <pre><code>public class GenericTest {
  
  
      static private interface Base {
      }
  
  
      static private class A implements Base {
      }
  
      static private class B implements Base {
      }
  
      static private class C extends B {
      }
  
      private List&lt;Class&lt;? extends Base&gt;&gt; types;
      private List&lt;Base&gt; objects;
  
      public GenericTest() {
          types = new ArrayList&lt;Class&lt;? extends Base&gt;&gt;();
          types.add(A.class);
          types.add(B.class);
          types.add(C.class);
  
          objects = new ArrayList&lt;Base&gt;(Arrays.asList(new A(), new B(), new C()));
  
          for (Base base : objects) {
              if (base instanceof A)
                  test((A) base);
              else if (base instanceof C)
                  test((C) base);
              else if (base instanceof B)
                  test((B) base);
  
              for (Class&lt;? extends Base&gt; c : types)
                  if (base.getClass().equals(c))
                      test(c.cast(base));
          }
      }
  
      private void test(A a) {
          System.out.println("A");
      }
  
      private void test(B b) {
          System.out.println("B");
      }
  
      private void test(C c) {
          System.out.println("C");
      }
  
      private void test(Base base) {
          System.out.println("Base");
      }
  
      public static void main(String[] args) {
          new GenericTest();
      }
  }
  </code></pre>