2

I've got several different classes that implement the interface as shown below:

public class Class1 implements interface1 {

    @Override
    public void action(){
        //do somethod
    }
}

public class Class2 implements interface1 {

    @Override
    public void action(){
        //do somethod
    }
}

public class Class3 implements interface1 {

    @Override
    public void action(){
        //do somethod
    }
}

My question is, then when I initialise those classes, how can I call the action method for each one of them in a loop. I was thinking of storing them in a list and then walk through it and call them, but I couldn't make it.

Avtontom_
  • 305
  • 2
  • 6
  • 19
  • 2
    a List should do the job –  Nov 17 '17 at 12:14
  • I was thinking if it's a good method of doing so, or it is better if I create a parent class? – Avtontom_ Nov 17 '17 at 12:15
  • 1
    it works so keep it simple and don't create a useless parent. see also https://stackoverflow.com/questions/56867/interface-vs-base-class?rq=1 –  Nov 17 '17 at 12:16
  • 1
    @Avtontom_ creating a parent class wouldn't really make a difference in your simple example. Just using the first common parent which contains the methods you need will be the best solution. – kalsowerus Nov 17 '17 at 12:17
  • Got it, thanks to both of you. – Avtontom_ Nov 17 '17 at 12:25
  • Do you want to implement the "Class" or do you want to create three objects and initialise the objects. I think you are confused by the difference between a class and an object. If you want to call action() when the object is created (initialised) just call it from the constructor. – rghome Nov 17 '17 at 13:31

1 Answers1

1

When you have different classes that implements certain interface you may deal with them in polymorphic way.

First, lets change your code to make it according to java code conventions:

public interface Interface {

      void action();
} 

public class ClassOne implements Interface {

    @Override
    public void action(){
        System.out.println("class one action");
    }
}

public class ClassTwo implements Interface {

    @Override
    public void action(){
        System.out.println("class two action");
    }
}

public class ClassThree implements Interface {

    @Override
    public void action(){
        System.out.println("class three action");
    }
}

After that create list and add new instances to it and call method action() for each element in list, you don't need to know actually what class instances presents in list, you only need to know that all of them implements certain interface.

List<Interface> list = new ArrayList<>();
list.add(new ClassOne());
list.add(new ClassTwo());
list.add(new ClassThree());
for(Interface instance : list) {
    instance.action();
}

output will be:

class one action
class two action
class three action

P.S. you may even write this in one line with java 8/9

java 8

Arrays.asList(new ClassOne(), new ClassTwo(), new ClassThree()).forEach(System.out::println);

java 9

List.of(new ClassOne(), new ClassTwo(), new ClassThree()).forEach(System.out::println);
fxrbfg
  • 1,756
  • 1
  • 11
  • 17