If you want to return something from a method, the method signature will include the type of the returned object. This can be any valid Java type.
The common superclass of all objects is the class Object. Thats why you can return anything from a method declared with Object as its return type. Generally tough, consumers can´t do much if they see an Object being returned from a method.
Consider the following example:
public Fruit getFruit() {
return fruit;
}
Here, you can return anything that is a fruit. It can be a Fruit, or any object that extends it, like an Apple or a Banana. Consumers will enjoy this return type a lot more, since it is something specific.
Now, you know that you get back a Fruit from this method. What you dont know: What type of fruit it excactly is. But what you know is that you can do anything with that fruit that can be done with all fruits, for example eat it:
public class Fruit {
public void eat() {
System.out.println("Mhh, tasty!")
}
}
If you want to return objects that are not from the same class, try to introduce a common superclass or interface that extract shared data or operations. If you cannot find any, write two seperate methods that each return their independent object.
Also, you can write a wrapper class that encapsulates your returned objects. That class can then hold more than one object.
public Result getResult() {
return new Result(a, b);
}
public class Result {
private A a;
private B b;
// remainder ommitted
}
Objectand that's your return type. But what issue you are facing?Objectsyou shout be fine. Not sure why you didn't just run this and asked here?