'Polymorphic Methods in Java (from recitation) [duplicate]

This is for an ungraded recitation assignment for a 200 level CS course at my University. I am just asking for my own understanding.

In the following code, why does line 4 print B and A instead of B and B? Also, in Intellij I am told that the objs after the A in A.show(D obj) and B.show(B obj) are not used, while the A.show(A obj) and A.show(B obj) are used, even though they are seemingly both used the same way; just to identify which overloaded method to use.

public class Polymor {
    class A {
        public String show(D obj) {
            return ("A and D");
        }
        public String show(A obj) {
            return ("A and A");
        }
    }
    class B extends A {
        public String show(B obj) {
            return ("B and B");
        }
        public String show(A obj) {
            return ("B and A");
        }   
    }
    class C extends B {}
    class D extends B {}

    public static void main(String[] args) {
        Polymor outerclass = new Polymor();
        A a1 = outerclass.new A();
        A a2 = outerclass.new B();
        B b = outerclass.new B();
        C c = outerclass.new C();
        D d = outerclass.new D();
        System.out.println("1:" + a1.show(b));
        System.out.println("2:" + a1.show(c));
        System.out.println("3:" + a1.show(d));

        System.out.println("4:" + a2.show(b));
        System.out.println("5:" + a2.show(c));
        System.out.println("6:" + a2.show(d));

        System.out.println("7:" + b.show(b));
        System.out.println("8:" + b.show(c));
        System.out.println("9:" + b.show(d));
    }
}

I understand all of the outputs except for the ones I mentioned. I have searched online for this unused parameter issue but cannot find anything.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source