Q:What is the result of attempting to compile and run the program? A: prints: m3Cm2Bm1A
My doubt here is in the line x, we have a method called m2(x); Here x is a subclass reference ie,new C(). But there is no overloaded version of m2 accepting a instance of class C. How can superclass references substitute for Subclass reference??
(added [C0DE][/C0DE tags, and removed Graemlin) [ August 31, 2004: Message edited by: Barry Gaunt ]
An object of type C isa B isan A, by virtue of the class hierarchy. So the object can be passed to a method taking a reference to an object of types C, B, or A. Now in the case of method m2 there are two possibilities only: m2(A x) or m2(B x). The compiler chooses the method with the most specific type (the closest one to C in the hierarchy), and that is type B. So m2(B x) gets called.
By the way I believe this is one of Dan Chisholm's questions so please mention that when posting questions from his mock exams.