This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
the point here is class C inherits the method show() from class B. and second point is call to a method depends on the actual object in the reference at runtime (but plz make a note the method declaration should also exist in the reference type orelse it will give compilation error).
so here a and b references have C object so the ouput will be B followed by B b'coz the method in class B which is inherited by class C and is called at runtime.
>>B b = new C(); >>b.show(); The method invocation depends on the type of object stored rather than the variable type B.So in the above code B b = new C(), the compiler will first look for the presence of show() method in Class C,if it doesn't finds it, it starts searching in the parent class. So u get B printed in this case.
A a = new C(); a.show(); In this case also samething happens , the search doesn't depend on variable stored but the type of object referred.