Java Overload method with inherited interface

主宰稳场 提交于 2019-12-01 06:55:26

Because the compiler only knows that a is an instance of IA. Overloads are determined at compile time based on the compile-time types of the expressions involved, and the compile-time type of a is IA.

(Compare this with overriding, where the method implementation is chosen at execution time based on the actual type involved.)

The line

 IA a = new myClass();

defines the object a as type IA and that is all the compiler knows. It cannot assume that a is also IB because it is entirely possible for this to be true:

 public class MyClass2 implements IA{}

 IA a = new MyClass2();
 method(a);

in which case a is NOT an IB as in your example. So the compiler makes no assumptions about the type other than what you provide. So it has to call the method that accepts IA.

Because you are passing "a", wich is an IA argument.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!