-1

I have seen that one can use ArrayList<Object> objectList = new ArrayList<Object>(); in order to generate a list that may contain different kinds of objects (such as object A and object B). I also have seen answers that state one can search what kind of objects are contained in that list by using

for (Object obj : objectList){
     if(obj.getClass() == A.class){
         doSomething();
     } 
}

However, its has been mentioned that this is not a very elegant approach. On the other hand, if I use an abstract class or an interface I need to have the same methods for object A and object B. For example if A.get() returns an integer, B.get() must also return an integer. But what I want is that the method making use of my objectList will do something different for different objects contained in the objectList. So A.get() may return an integer, B.get() may return a string.

So what I want to ask you guys is what is the best way to do this? Its not difficult to write a "hack", but I want to write good code.

Thanks.

TopCoder
  • 1
  • 1
  • I think a generic object can be used, if you have same method name – Limetics May 26 '16 at 09:41
  • use instanceof operator => `if( obj instanceof A)` instead of `obj.getClass() == A.class`, see this answer: http://stackoverflow.com/questions/7526817/use-of-instance-of-in-java – krokodilko May 26 '16 at 09:43
  • Why do I get a -1 rating for this post? I do use proper grammar, I did not just copy some large section of code into my question, I made my question well understandable (all of you understood my question immediately). So whats the matter?! Thx. – TopCoder May 26 '16 at 15:25

2 Answers2

1

If your method doSomething() is highly relying on the type of object, you could move it to those objects and use an interface.

public interface Foo {
    void doSomething();
}

public class A implements Foo {
    public void doSomething() {
        // do something
    }
}


for (Foo obj : objectList){
    obj.doSomething();
}

How elegant this is strongly depends on what is done in doSomething(). If it is really different for each object, this approach might work. If it is similar, try identifying only the differences and encapsulate them in the interface like shown above.

André Stannek
  • 7,773
  • 31
  • 52
  • thanks. the problem is I have to write into a large section of code that has been done by somebody else - and its not such a good section of code so I try my best not to make it worse. – TopCoder May 26 '16 at 10:30
0

Like this?

public  class BaseClass {
    public void method() {}
}

2

public class ChildOne extends BaseClass {

    public void method() {
        System.out.println("inside ChildOne");
    }
}

3

public class ChildTwo extends BaseClass {

    public void method() {
        System.out.println("inside ChildTwo");
    }
}

4

public class Client {

    public static void main(String[] args) {
        List<BaseClass> obj = new LinkedList<>();
        obj.add(new ChildOne());
        obj.add(new ChildTwo());

        for (BaseClass b : obj) {
            ((BaseClass) b).method();
        }
    }
}
T. Claverie
  • 11,380
  • 1
  • 17
  • 28
bananas
  • 1,176
  • 2
  • 19
  • 39