Typecast an object to generic
<p>I am developing an application where i need to send an object to another class and back to calling class. My code is....</p>

<pre><code>class Generic{
public static byte[] SerializeObject(Object o) {
        byte[] buffer = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        `enter code here`try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            oos.close();
            buffer = baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    public static HelloWorld DeSerializeObject(byte[] buf) {
        HelloWorld d2 = null;
        try {
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(buf));
            d2 = (HelloWorld) ois.readObject();
            ois.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        return d2;
    }
}
</code></pre>

<p>And i am calling this methods from a different activity like</p>

<pre><code>HelloWorld helloworld=Generic.DeSerializeObject(bytearray);
</code></pre>

<p>Now i want to write a generic method to send this object and typecast in this method generically.Is there any method to achieve genericity in JAVA?</p>
