2

Why sifter(C[]... c2) is called while calling only sifter() with no argument. I think here should be ambiguity in methods but the code is compiling and giving the output as -41.

package abc;

class A {}
class B extends A {}
class C extends B {}

public class ComingThru {
    static String s = "-";
    public static void main(String...a) {
        A[] aa = new A[2];
        B[] ba = new B[2];
        sifter();
        sifter(aa,ba);
        System.out.println(s);
    }

    static void sifter(A[]... a2) {
        s += "1";
    }

    static void sifter(C[]... c2) {
        s += "4";
    }

    static void sifter(B[]... b1) {
        s += "2";
    }   
}
1
  • 14: invokestatic #29 // Method sifter:([[LC;)V this is the byte code for line sifter(); and from there we can figure out it is converting method sifter() to sifter(C[]... c2) Commented Jan 8, 2014 at 6:18

2 Answers 2

2

Referencing the Java Language Specification

Section 15.12.2.4. Phase 3: Identify Applicable Variable Arity Methods, it states towards the end that:

If no applicable variable arity method is found, a compile-time error occurs.

Otherwise, the most specific method (§15.12.2.5) is chosen among the applicable variable-arity methods.

Now looking at section 15.12.2.5 right below in the same page, it states:

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

Therefore, it shows that all of the 3 method declarations are applicable for the sifter(); call. However, static void sifter(C[]... c2) is the most specific since you can call the other two methods (A[]... and B[]...) with the same input and there would be no compilation error.

Sign up to request clarification or add additional context in comments.

2 Comments

I have added another method static void sifter(Integer[]... i), Now it is showing compile error, so why sifter is checking for specific arguments though it is accepting no arguments.
@hemkaran_raghav static void sifter(Integer[]... i) is its own maximally specific method. So now there are 2 maximally specific methods Integer[]... and C[]... -- At compile time, which method one to choose is ambiguous as both of them apply and both of them are maximally specific. Therefore, it raises a compilation error. (Reference section 15.12.2.5)
1

I think it is related to the hierarchy Level, It is calling that method whose parameter is the lowest in order of Inheritance. You can take a example, if we replace B with C then it will the method with B[]...b1 as argument.



    package abc;

    class A {}
    class C extends A {}
    class B extends C {}

    public class ComingThru {
        static String s = "-";
        public static void main(String...a) {
            A[] aa = new A[2];
            B[] ba = new B[2];
            sifter();
            sifter(aa,ba);
            System.out.println(s);
        }

        static void sifter(A[]... a2) {
            s += "1";
        }

        static void sifter(C[]... c2) {
            s += "4";
        }

        static void sifter(B[]... b1) {
            s += "2";
        }   
    }


Now the output will be -21

3 Comments

Yes, this will be the output. But what is the reason behind it?
Lowest in the hierarchy level. If we add another method accepting Object as varagrs, still it will call method with B.
Yes, your correct but why is that. If we look at the method prototype sifter() there is no method defined for such prototype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.