Polymorphism and method overloading

前端 未结 5 753
深忆病人
深忆病人 2020-12-25 11:52

I have a quick and straighforward question:

I have this simple class:

public class A
{
    public void m(Object o)
    {
      System.out.println(\"m         


        
5条回答
  •  一整个雨季
    2020-12-25 12:35

    First of all, it actually calls m(Number).

    It happens because both methods are applicable, but m(Number) is the most specific method, since any argument of m(Number) can be passed to m(Object), but not vice versa.

    If you replace m(Object) by m(String) (or add another method such as m(Date)), compiler would report ambiguity, since the most specific method can't be identified.

    See the section Choosing the Most Specific Method in the Java Specification.

提交回复
热议问题