0

I have an interface

public interface Inter {
    int interAttr = 0;   
}

And two classes that implements it, each one with an additional attribute and each one of different type.

@AllArgsConstructor
public class Impl1 implements Inter {
    public int impl1Attr;
}

@AllArgsConstructor
public class Impl2 implements Inter {
    public String impl2Attr;
}

And I have list of Inter objects that are a mix of Impl1 and Impl2

public class main {
    public static void main(String[] args) {
        Impl1 i11 = new Impl1(0);
        Impl1 i12 = new Impl1(1);
        Impl2 i21 = new Impl2("zero");
        Impl2 i22 = new Impl2("one");
        List<inter> implList = new ArrayList<Inter>();
        implList.add(i11);
        implList.add(i12);
        implList.add(i21);
        implList.add(i22);
        for (Inter el : implList){
            // this of course works
            int a = el.interAttr1;
            // But how do I get access to impl1Attr and impl2Attr?
        }
    }
}

Of course I can query i11.impl1Attr or impl21.impl2Attr in the body of the main But is there a way for me to have access to the attributes of the different implementations in the list in the for loop?

JacoSolari
  • 1,226
  • 14
  • 28
  • 1
    I'd say - no, there is no easy way, what exactly you're trying to do? – Iłya Bursov May 18 '20 at 18:37
  • If you wanted to access each individual class' specific attribute, then I suggest avoid putting them in a list which takes the interface as a type. The list is catering for objects implementing the interface, not their class specific attributes. – TM00 May 18 '20 at 19:21
  • @TM00 this is just a very simplified example from a much larger project. Cannot change data structure very easily. – JacoSolari May 19 '20 at 08:47
  • @IlyaBursov I am writing some input validation for an optimization routine. It happens that the input class contains a mixture of two different implementations of the same interface, in a list. – JacoSolari May 19 '20 at 08:49
  • 1
    @JacoSolari if object can be validated, then interface should have method validate, then each object will implement it and your loop just call this method – Iłya Bursov May 19 '20 at 14:43

1 Answers1

0

I'm probably oversimplifying the problem, but is instanceof + casting not an option?

for (Inter el : implList) {

    int a = el.interAttr;

    if (el instanceof Impl1)
        System.out.println(((Impl1)el).impl1Attr);

    if (el instanceof Impl2)
        System.out.println(((Impl2)el).impl2Attr);
}
Thomas Portwood
  • 1,031
  • 8
  • 13
  • This is what I wanted and it works. I have tried `el.getClass().isInstance( Impl1.class )` but was not working. Do you maybe know the difference between your approach and this one? – JacoSolari May 19 '20 at 08:46
  • 1
    I did not before now, but here is a great answer to that exact question https://stackoverflow.com/questions/8692214/when-to-use-class-isinstance-when-to-use-instanceof-operator – Thomas Portwood May 19 '20 at 19:16