views:

294

answers:

3

I am looking for a way to serialize a class in Java into it's superclass, and then convert it to a byte array and send it over a network.

Here is an example:

class SuperClass {
    public int number1;
}

class SubClass extends SuperClass {
    public int number2;
}

I don't think it's possible to simply cast an object of SubClass into SuperClass and then serialize it, or am I wrong? Is there some way to do this "conversion" without having to manually copy all the inherited values from a SubClass to a new SuperClass?

EDIT: Well, I realized I was shooting myself in the foot by doing it the way I originally thought of, so what I decided to do was simply serialize SubClass, and use it on both sides of the network connection. It's the simplest thing to do, and I don't have to worry about casting and inheritance.

A: 

I don't think it's possible to simply cast an object of SubClass into SuperClass and then serialize it

Have you tried it yet... Thats the easiest way to see if something is possible...

If you want to get a byte array for a serialized object, Serialise to a ByteArrayOutputStream.

CuriousPanda
A: 

I think wording of your title is not what you really want.

I am looking for a way to serialize a class in Java [...] and send it over a network.

The above problem sounds more realistic.

You need something like this:

public class SuperClass implements Serializable {
   ...
}

public class Messenger {
   ...

   public void send (SuperClass obj) {
       ObjectOutputStream oos = new ObjectOutputStream (socket.getOutputStream ());
       oos.writeObject (obj);
       oos.close ();
   }

   public SuperClass receive () {
      //some work with ServerSocket#accept() should be here
   }
}

You can transfer with above code objects which are instances of SuperClass or any of its subclasses. But if in one of subtypes you want to transfer only part of an object and the rest part should be set "on the other side" then you should add methods readObject () and writeObject () to that superclass. Read some tutorials about serialization for more details (link).

UPD:

If program on the other side which receives serialized object doesn't already load class of received object then you'll get an exception.

That's why you need to perform next steps in order to transfer object of unknown sublass:

  1. Transfer class of the object and its name to the other side.

  2. Load transferred class on the other side (i.e. Class.forName (classname)).

  3. Now send the object.

The most difficult part is 1st step. Think how to do that. I don't have necessary example now but I'll probably post it latter if you won't find the solution by yourself till then.

Roman
Well, yes, but the client on the other end does not know of the class called SubClass, only SuperClass. I tried casting from SubClass to SuperClass, and it works, but the object is still a SubClass. I want to convert an object of SubClass, to SuperClass, and then serialize it.
Bevin
Ah, now it's clear. Add this comment to your question. You need another approach. I'll try to update my answer but I don't have much time now.
Roman
+1  A: 

In concepts of OO: "SubClass" is always a "SuperClass". So there is no need to cast/copy/convert. Just use it as a SuperClass.

Willi