In my cache class, I’m going to have a function which writes the serialized version of an object (undecided type) to a file, something like this (generic function):
public <O> void write(O object) {
// ...
serialize(file, object);
// ...
}
Which works great, however, I’m unable to find a way to create a method which can return any object, like the write() method can take any object. Looking for something like this:
public <O> read() {
// ...
O object = unserialize(file);
// ...
return object;
}
Any suggestions on how to accomplish this is highly appreciated!
You specify the return type of type Object:
That way the return type will always be of type Object (since all objects are descendants of
Object), so they will be accepted.