1

Considering this part of a Java class,

private List<Object> components = new ArrayList<Object>();

private A a;
private B b;
private C c;

add(a, b, c);

public void add(Object... component){
    for(Object o : component) {
        components.add((CAST)o);
    }

}

I would like to cast my Object o in the list, by the good type.

The expected result here would be to have A a, B b, C c in my list.

I try with getClass(), but I didn't compile. How can I upcast Objects, considering that I already know the expected Java type?

edit:

I need to add a first object of type A, a second object of type B, a second object of type C, ... (I don't know the number of items to add) if a list so that I will be able to get the list containing N objects correctly typed. (gtt(0) would be typed as A, get(1) as B, and get(2) as C )

Because I don't know the number of objects and that I initially don't know the object's type, I delclared my list as Object.

I created a method (add in that case). It varargs will help me for looping in the collection because I don't know the number of args.

I would simply like to get the first object, the second object and the third object (and so on) in my varargs, and to simply push it in my list, but not as Object type, as A, B or C, depend of what I received!

I don't know if it's more clear.

4
  • I want a more specific list.. Commented Jun 19, 2012 at 22:12
  • So you know the type at cast time but not when you are creating the list, correct? Commented Jun 19, 2012 at 22:18
  • Exactly! I know the type at cast time, because the add varargs know the types.. Commented Jun 19, 2012 at 22:24
  • 1
    But you've defined it as a list of objects. Commented Jun 19, 2012 at 22:43

4 Answers 4

1

Your list already accepts Objects

private List<Object> components = new ArrayList<Object>();

why do you need to upcast?

Sign up to request clarification or add additional context in comments.

1 Comment

I want specific Objects.. I declared my list as Object because I don't already know the type..
1

First, you can never upcast in this case. The only place you REALLY need an upcast is when you're accessing a method specific to C, not in B or A; or specific to B not in A. (or neither in Object)

1 Comment

aren't you explaining downcast?
1

You seem to be misunderstanding how the runtime type of an object works. When you put an 'A' into a List it does not stop being an 'A' and become an Object. If you take it out later and cast it back to an 'A' it is still the original runtime type. There is nothing gained by casting it before putting it in the list.

Consider this

Object str = "FOO";
List<Object> list = new LinkedList<Object>();
list.add(str);
String str1 = (String) list.get(0);
System.out.println(str1.length());

Prints '3'. "FOO" never stopped being a String just because the reference type of the pointer to it was Object.

4 Comments

Yes, maybe my question was not clear. I want to insert a String type in a Object list, and be able to get it as String. The thing is that I don't know the type of the collection, so I created a add method which will loop in my collection and I would like to add it an my Object list.
@Pier-alexandreBouchard you must first understand the difference between upcasting and downcasting
Perhaps you could give us a clearer code example then? In the example you posted, the cast does nothing, so we can't tell what you're trying to achieve.
Yes, the run-time type of the object does not change because you put it in a list of object. It is still an A or B or C. There is no need to cast it there. When you get(1); you will get back a B regardless of if you do that cast.
1

To be honest, I agree with everyone above, that it doesn't make much sense upcasting in the example you gave. Since you declared that the list will contain Objects, you will have to do the cast again when retrieving the object from the list. However, there is allways the type comparison operator instanceof which you could use.

public void add(Object... component){
    for(Object o : component) {
        if(o instanceof CAST)
            components.add((CAST)o);
    }
}

Comments

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.