0

B extends A, C extends A

trying to change:

   public void convertA(A a) {

    AClass clazz = new AClass();

    if (a instanceof B) {
        clazz.setX(convertBtoX(a));
    } else if (a instanceof C) {
        clazz.setY(convertCtoY(a));
    }

}

to

public void convertAbis(A a) {

    AClass clazz = fill(a); //ERROR NO SUITABLE METHOD FOUND FOR FILL

}


public AClass fill(B b) {
     clazz.setX(convertBtoX(b));
}

public AClass fill(C c) {
    clazz.setY(convertCtoY(c));
}

results in a "no suitable method found for fill" error. How could I remove the "instance of" switch? A, B, C are Data Trasfer Object and should not be aware about X Y and viceversa so I can't simply add fill methode to A,B,C classes.

Suggestions?

Francesco Umani
  • 69
  • 4
  • 10

2 Answers2

1

fill(a) will not automatically match the B and C args, but looking for fill(A a).

To sove your problem, try below.

First create a abstract function void fill(AClass c) in class A.

Create this function in class B:

public void fill(AClass clazz){
    clazz.setX(convertBtoX(this));
}

And do almost the same to class C.

public void fill(AClass clazz){
    clazz.setY(convertCtoY(c));
}

Then use as this:

void convertABis(A a){
     AClass clazz = new AClass();
     a.fill(clazz);
}
ch271828n
  • 15,854
  • 5
  • 53
  • 88
  • "A, B, C are Data Trasfer Object and should not be aware about X Y and viceversa so I can't simply add fill methode to A,B,C classes." – Francesco Umani Mar 13 '15 at 09:29
1

I think the problem is that A is not a subclass of B or C, but there are B and C that are subclasses of A. So, passing an object of type A to methods fill is not possibile.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106