SYOB SYOT
SYOB SYOT

Reputation: 1190

How to return different types of object from a method in java?

I want to ask you how a single method can return different types of objects in java?

lets say we have a class "a". class a is parent class of classes "b" and "c". lets say we have an array list defined as:

java.util.ArrayList<a> list;

so this list can contain objects of class a, class b and class c. lets say we create one object of class b, and one object of class c, and we add them to list.

now, i want to create a method like this:

public ? get_element(boolean first)
{
    if (first)
    {
        return list.get(0);
    }
    else
    {
        return list.get(1);
    }
}

my problem is, that i do not know what to put at the place of "?" in this method. if i put "a", so, "public a get_element", when element is returned it is always of type "a", no matter if element was originally of type "b" or "c" when it was added.

but i want that element returned is of type of element added. so, if i add element of type "b" at index 0 of the list, and element of type "c" at index 1, i wish when i call get_element method, that if i provide parameter equal to "false", that returned element is of type "c", and if i provide true when i call it, that returned element is of type b.

of course, i can cast returned element to the type i wish. but this is just simple example i wrote, but i for real have much more complex situation. i have about 10 classes which all extend one class, and array list which can contain any of those 11 types of objects, and they can be arranged in any way, so i can not know what type of element is at which position, unless i create a code which will try to cast to all types, one by one until it finds the one to which it can cast.

but i want to know, is there some kind of more easy way to do so?

to simplify my question, i wrote this .java file:

public class test
{
    public static class a{}
    public static class b extends a{}
    public static class c extends a{}
    public static java.util.ArrayList<a> list = new java.util.ArrayList<a>(2);
    static
    {
        b first_object = new b();
        c second_object = new c();
        list.add(first_object);
        list.add(second_object);
        b first_returned_object = get_element(true);
        c second_returned_object = get_element(false);
    }
    public static ? get_element(boolean first)
    {
        if (first)
        {
            return list.get(0);
        }
        else
        {
            return list.get(1);
        }
    }
}

my problem is that i do not know what to put before "get_element" in get element method.

what should i put in order that i can save object of type b in "first_returned_object", and object of type c in "second_returned_object"?

of course, this is just example, i would not use these classes, because they are useless, but i wrote this to make my question as simple as possible.

my real classes where i have this kind of problem have 5000+ lines, so i could not include those here, so i wrote the simplest possible version which demonstrates my problem.

any suggestions?

thanx.

post scriptum: to caps lock haters: your hate is bidirectional here. ;)

Upvotes: 3

Views: 9518

Answers (1)

SkrewEverything
SkrewEverything

Reputation: 2523

You can use Generics to some what make the return type dynamic.

Try this one:

public static <Type> Type get_element(int index)
{
    String s = list.get(index).getClass().toString(); // returns "class class_name"
    String[] split = s.split(" ");
    Class<?> theClass = Class.forName(split[1]); // You need "class_name"

    return (Type) theClass.cast(list.get(index));
}

or simply:

public static <Type> Type get_element(int index)
{
    return (Type) list.get(index);
}

Upvotes: 3

Related Questions