Hi all,
I want to have a base class, BaseConnect
, which contains an OutputStream
and children classes ObjectStreamConnect
and DataStreamConnect
. In my BaseConnect
class I have OutputStream os;
And in my Two children classes I have the constructors that do "os = ObjectOutputStream(...)
" or "os = DataOutputStream(...)
", respectively.
Since ObjectOutputStream
s have a writeObject(Object o)
method and DataOutputStreams
do not, it seems that I cannot have my ObjectStreamConnect
class do a "os.writeObject(object)
" since the parent class, OutputStream
, does not have writeObject(Object o)
.
I'm sure this is code-smelly, and I'm wondering how to handle it.
My thoughts:
I thought of making the method that contains os.writeObject
abstract, so that ObjectStreamConnect
could implement it differently, but then I realized that DataStreamConnect
would also have to implement it, which it does not need to.
I also do not want to just get rid of the parent and have the two classes implement everything separately, because they do have a lot of the same methods with the same implementations.
Please help. For some reason, the answer to this problem is not coming to me.
jbu
edit: I can't paste entire code but it goes something like this:
public class BaseConnect {
OutputStream os;
...
}
public class ObjectStreamConnect extends BaseConnect {
public ObjectStreamConnect () {
...
os = new ObjectOutputStream(socket.getOutputStream);
}
public void writeObject(Object o) {
os.writeObject(o);
}
}
public class DataStreamConnect extends BaseConnect {
public DataStreamConnect () {
...
os = new DataOutputStream(socket.getOutputStream);
}
}