0
Need to return differnent objects in same method?is it Possible to return?

   public class Smoketest(){     
    protected Object getLabTopology() {
        if(labTopology == null) {
            return labTopologyAbstarct;
        } else {
            return labTopology; 
        }
    }
}

public test extends Smoketest()
{

getLabTopology();
 }

//Need to do the operation directly without casting .is it possible?

3
  • 3
    Yes, you can as any object's super class is inhernetly Object and that's your return type. But what issue you are facing? Commented Mar 30, 2019 at 19:01
  • Uh, yeah; whatever return statement it hits first will terminate the function and return the object. As long as those are both Objects you shout be fine. Not sure why you didn't just run this and asked here? Commented Mar 30, 2019 at 19:03
  • Both are different classes , i dont want to do inherit . how can i return .. i am getting error ((Object) getLabTopology()).releaseEdpt(); Commented Mar 30, 2019 at 19:06

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer 1+ for the good efforts to explain concepts, concepts that are complex to newbies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.