0

I got a short question about polymorphism in java: Here are two class hierachies and the exercise is to tell what the the code is going to print out.

class All { /* ... */ }
class Most extends All { /* ... */ }
class Special extends Most { /* ... */ }

class Top {
    public void m( All p ) { System.out.print("A "); }
}
class Middle extends Top {
    public void m( All p ) { System.out.print("M "); }
    public void m( Special p ) { System.out.print("L "); }
}
class Bottom extends Middle {
    public void m( Most p ) { System.out.print("V "); }
    public void m( Special p ) { System.out.print("X "); }
}

class Test {

    public static void main(String[] args) {
        All all = new All();
        Most most = new Most();
        Special special = new Special();

        Top x = new Middle();
        Top y = new Bottom();
        Middle z = new Bottom();

        x.m( most );
        x.m( special );

        y.m( all );
        y.m( special );

        z.m( all );
        z.m( most );
    }
}

I pasted the code into an Eclipse .java file and it printed out 6x M, so M for each method. Could anyone help me understand?

Apparently the declared type of the argument of the calling reference and the argument matter and nothing else (according to the script) but in that case, why doesn't x.m(special) print out "L" since Special is the argument type of the method?

PS: My very first question on stackoverflow, really hope someone can help me understand :)

3
  • 1
    It would help a lot if your question would match your code. I don't see a definition of "x" nor do I see a use of "all" and "most". Would you please clarify? Commented Feb 29, 2020 at 12:38
  • 1
    When a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. In this case, x.m(special) prints 'M' as x is the superclass reference variable (of Top class) that has no knowledge of what members are present in the Special class. But if you create an object of Middle m = new Middle; and then call m.m(special), you'll get L. Commented Feb 29, 2020 at 13:00
  • 2
    "why doesn't x.m(special) print out "L"?" x is defined as Top x = ..., this means that x can chose only from method signatures available in Top type which gives us only public void m( All p ) signature. Polymorphism kiks in when JVM needs to find body of method with already selected signature, so here it will look at actual type of object held by x which is x = new Middle(); and in the Middle class it will search for implementation for public void m( All p ) method. Commented Feb 29, 2020 at 13:40

1 Answer 1

2

First of all, if you changed the signature, you might as well change the name of the method. that will make things a lot clearer.

x and y being declared Top class, they have only one method to be invoked compiler-wise: m(All). The implementation of it they get called (overiden method) is from Middle, thus they print M.

Middle z has 2 methods to invoke compiler wise, m(All) and m(Special). Compiler hooks up the m(All) call because that's all it can do with a Most arg. Since Bottom inherits without overriding m(All), it also print M.

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

Comments

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.