Recently, I ran into a problem with the API and implementation, where the following type of code appeared:
API - abstract class:
public abstract class A {
public A sum(A a) {
System.out.println("A.sum(A) called");
return null;
}
}
The implementation is a simple class:
public class B extends A {
public B sum(B b) {
System.out.println("B.sum(B) called");
return null;
}
}
When it comes to using it, I write:
public class Main {
public static void main(String args[]) {
B b = new B();
A basa = new B();
b.sum(b);
basa.sum(b);
basa.sum(basa);
}
}
Result:
% java Main
B.sum(B) called
A.sum(A) called
A.sum(A) called
I understand that the sum B does not redefine the sum A, since its signature is different, but I would like to ensure an effective implementation of the sum for objects of effective type B. I think that this design is quite classic and would like now to develop my API and implementation so that it is effective.
, (A a) be , b Of B sum (B) super, , instanceOf ( , , "" .