title: How to cast from class type to interface
body: <p>I have an interface which is implemented by few classes. Based on the full name of the class I want to initialize the class objects. </p>
  
  <p>Interface,</p>
  
  <pre><code>public interface InterfaceSample{
  }
  </code></pre>
  
  <p>Class files,</p>
  
  <pre><code>public class ABC implements InterfaceSample{
  }
  public class XYZ implements InterfaceSample{
  }
  </code></pre>
  
  <p>A sample test class,</p>
  
  <pre><code>public class SampleManager{
  public static InterfaceSample getInstance(String className) {
      InterfaceSample instance = null;
      try {
          instance =  (InterfaceSample) Class.forName(className);
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      return instance;
  }
  
  } 
  </code></pre>
  
  <p>I am getting the following error,
   <b>    </p>
  
  <pre><code> "Cannot cast from Class&lt;capture#1-of ?&gt; to InterfaceSample"
  </code></pre>
  
  <p></b></p>
  
  <p>How can I initialize the classes based on its name.</p>