Which method is called when multiple overloads are legal?

Say you have an interface Aand an interface B. Let's say a class Spriteimplements both interfaces.

Say another class that has a method foo(A object)and also has a method foo(B object).

Both are called when I pass the instance to the Spritemethod foo()? If not, does it take precedence?

+3
source share
4 answers

When the method is overloaded (as used here), the invocation method is allowed at compile time based on the (declared) type of the variable containing Sprite.

, , , .

+8

. .

+4
   interface A {}

   interface B {}

   class Sprite implements A,B {}

   class Test{
      void foo(A a){}
      void foo(B b){}

      void test(){
       Sprite s = new Sprite();
       foo(s); // <-- compile time error (The method foo(A) is ambiguous for the type Test)
      }
   }
+3

; . .

, , . , .

, , , , , . A , B, A B. , A () B. , , , - " " , " " . . .

0

Source: https://habr.com/ru/post/1793853/


All Articles