Fundamentally, here is how I understand polymorphism:
Type varname = new Class();
When the program is run, the compiler will, when trying to determine which method(s) it can run, look at the right (Class()), not at the left (type).
So, in this example, it will look at:
Code: Select all
A x = new --> B() <--; B y = new --> B() <--; B z = new --> C() <-- ;
But the class of x is B. The type is A. Am I confusing class and type? Based on my current knowledge, I'd say x.mA() is valid, x.mB() is valid, y.mA() is valid, z.mC() is invalid because there is no polymorphism taking place (no decision between several overridden methods), z.mB() is also not polymorphic...In this case, x.mB() is invalid call. It will not even compile because the class of x is A, which does not contain method mB().