Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Type of object?

Name: Anonymous 2010-04-01 11:15

Sup /prog/riders?

I have a common problem (I guess).
I have an abstract class "X" and two classes ("A" and "B") that inherit from it. Also I really need a list that can handle both of them.

List<X> mylist;
mylist.add(A);
mylist.add(B);

Now I need to pick out all items of type A and do something with them, possibly using methods only available in class A and NOT in X or B. In ENTERPRISE JAVA there is "instanceof" but using that sounds just as hacky as saving a type in every object.


Is there a CLEAN way of determining if the object from the X-list is of type A?
Not Java-specific, rather OOP in general.

Name: trackpads are shit 2010-04-01 12:19

>>12,14
Anyway. It doesn't matter it's doing a gazillion of, it'll still do a gazillion. In your case it will now do a gazillion function calls. In the other way, X's function will switch for whichever type it encounters.

Anyway, why are you being a shit? We tried to help (you know what /prog/ are like), we gave you some perfectly valid answers, you said 'Thanks for nothing' and then proceeded to say "This is why I'm right and you're wrong" even though you were the one asking the question in the first place.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List