Wentinn Liao
Wentinn Liao

Reputation: 11

Can a Java method return different types based on conditions in the method?

Is it possible to have a method return different types based on conditions? Never learned Java extensively but I researched generics and it doesn't seem to apply if the dependency on the parameter is not obvious. Basically something like

public ????? func(Object obj)
{
    if(obj instanceof class1)
    {
        return new Integer(1);
    }
    else if(obj instanceof class2)
    {
        return "asdf";
    }
    else
    {
        return new Double(1.7);
    {
}

I can guarantee that both any parameters passed in and anything returned will always be an object if that makes things easier. Thanks for the help

Upvotes: 0

Views: 8479

Answers (3)

rb612
rb612

Reputation: 5563

You can declare the method to have a return type of Object, but that's about it.

A big reason why strongly-typed languages like Java are so nice is because they guarantee that the type of the object you return is an instance of the type that's specified, no matter how complex your code gets. If what you get out of calling the method could be an Integer, a String, or a Double, the only thing that they really have in common is that they're Objects. There's not too much use for that for most cases. You could check the return type in the calling method to see if it's an instance of the class you're looking for and then do a cast, but this would not be a good use of object-oriented programming.

A better way is to create an interface that says what all objects that are returned from the method should do, and then you can return different types of objects which implement that interface (or alternatively, extend some common superclass).

For example, say you wanted to return different Animals based on different conditions. You can absolutely do this, since Dog, Cat, and Horse are all Animals. (I would suggest avoiding instanceof checks like this, but still works as an example):

public Animal func(Object obj)
{
    if(obj instanceof class1)
    {
        return new Dog();
    }
    else if(obj instanceof class2)
    {
        return new Cat();
    }
    else
    {
        return new Horse();
    {
}

Upvotes: 7

vipcxj
vipcxj

Reputation: 1038

Just return Object. That's what you are doing. Of course, you can use


public <T> T func(Object obj) {
   ...
}

But it is pointless. Because when you use this method, you are unable to decide what T should be. Because it is decided in the runtime. This code only useful when use it in a generic class with parameter T.

public interface CloneMe<T> {
   T cloneMe();
}

public class Bean implements CloneMe<Bean> {
   public Bean cloneMe() {
       return new Bean(this);
   }
}

Upvotes: 4

Sathiamoorthy
Sathiamoorthy

Reputation: 11570

You can return different types of objects in 2 ways.

Using generics method

    public <T> T func(Object obj){
        if(obj instanceof Class1)
        {
            return  (T) new Integer(1);
        }
        else if(obj instanceof Class2)
        {
            return  (T) "asdf";
        }
        else
        {
            return  (T) new Double(1.7);
        }
    }

This gives you an illusion of type safety when you call the method.

For instance, int i = func(new Class2()); compiles without a warning, but will throw a ClassCastException at runtime, as it tries to cast a String to an int.

Using Object class

    public Object func(Object obj){
        if(obj instanceof class1)
        {
            return new Integer(1);
        }
        else if(obj instanceof class2)
        {
            return "asdf";
        }
        else
        {
            return new Double(1.7);
        }
    }

Upvotes: 0

Related Questions