For SCJP most of the time question such as below is asked to find valid example of polymorphic method calls. But what should one exactly look for to find is it polymorphic use or not ?
abstract class A {
abstract void a1();
void a2() { }
}
class B extends A {
void a1() { }
void a2() { }
}
class C extends B {
void c1() { }
}
and:
A x = new B();
C y = new C();
A z = new C();
What are four valid examples of polymorphic method calls? (Choose four.)
A. x.a2();
B. z.a2();
C. z.c1();
D. z.a1();
E. y.c1();
F. x.a1();
Answer: A, B, D, F
Answer C won't compile (method isn't defined in the declared class). Answer E isn't polymorphic (method is definied in the declared class). All remaining answers uses a method which are either implemented (from an abstract definition), or overridden, or subclassed (of which all are polymorphic behaviours).
Here's an overview:
A x = new B();
C y = new C();
A z = new C();
A. x.a2(); // Method is overriden.
B. z.a2(); // Method is inherited.
C. z.c1(); // Won't compile. Method isn't defined in A.
D. z.a1(); // Method is implemented.
E. y.c1(); // Not polymorphic. Method is defined in declared class C.
F. x.a1(); // Method is implemented.