Creating objects using the Class class
<p>How do I instantiate an object using the Class class?</p>

<p>I would like to pass a Class object to a function and have that function return a new object of the class I pass in. </p>

<p>I will be doing casting after I get the object back. Also, are there any suggestions on design patterns for this sort of thing?</p>

<p>Here is some example pseudo code that shows what I want to do:</p>

<pre><code>Class Animal {
    String name;
}

Class Cat extends Animal {
    boolean hasTail;
}
</code></pre>

<p>MAIN:  </p>

<pre><code>Class myClass = Cat.class;
Animal createAnimal(myClass) {
    return new myClass;
}
</code></pre>

<p>Edit:</p>

<p>Also, is there a way to cast within my function before returning the new instance or am I stuck with the default Object type?</p>

<pre><code>return (myClass)myClass.newInstance();
</code></pre>
