Is there a way in Java to return different types with one declaration of a method?
public Object loadSerialized(String path) {
Object tmpObject;
try {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
tmpObject = (Object) ois.readObject();
ois.close();
fis.close();
return tmpObject;
} catch (FileNotFoundException e) {
return null;
} catch (Exception e) {
}
}
I want this method to return an Object and I cloud cast it to the right type at the function call. That was what i thought but it doesn't work like this. Do I need some kind of generic return Type to do this? What would be the best way to solve this problem?
catch
block doesn't return anything. – Culet