w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Java - Collection of abstract class, children contain disparate objects - Any "good" way to handle these wrapped objects?

You discarded in your question the "good" way of doing it. Generics will not help in this case. Yes, you are going to be cursed to a nasty place for the ugly use of type checking. You can disguise the type checking using a common instance member called, say, category and its corresponding accessor getCategory defined in MyClass. And them do a switch if you can (instead of multiple ifs) on getCategory(). However, the people who will condemn you could dislike ifs and switches no matter if they are checking for types or not. And they could also be smart and understand what you are trying to do. Anything but

for(MyClass e: collection )

is "bad".

Now, for software that seems to have no specification at all, you could be pardoned.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.