Multiple types of objects through an ObjectInputStream in Java
<p>I am trying to use ObjectStreams to send and receive data through a socket connection. I would be using the RMI interface however android doesn't support it. Through my implementation I have multiple kinds of objects that I would like to read through the stream. For example if the client wanted to disconnect from a specified room they would send a Disconnect object through, if they wanted to chat someone they would send a chat object through ect. </p>

<p>I know that you have to type cast to use the object in java as so:</p>

<pre><code>joinRoom = (Room) clientInput.readObject();
</code></pre>

<p>but if is there a way if i declare a generic object to tell what type it is and then determine how i will handle it?</p>

<p>maybe like this:</p>

<pre><code>Object obj;
obj = (Object) clientInput.readObject();
</code></pre>

<p>and then use?</p>

<pre><code>if(obj.getClass().equals(Room)){....}
if(obj.getClass().equals(Disconnect)){....}
</code></pre>

<p>thanks in advance.</p>
