views:

299

answers:

4

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

A: 

A is valid because you are calling a2() which has been overridden in B. C will not even compile because the c1() method is not present in class A. E is not valid because the type and the actual instances are same, no need for polymorphism. Similarly, you can figure other options.

fastcodejava
A: 

It would appear that, in this question, polymorphic method calls are calls to a method that belong to object that is higher up in the inheritance chain than the type of the object that is invoking it, and NOT a method that written or overloaded in the given object's class.

So since c and e are calling methods within their own class, they are not calling methods of classes higher up the chain - they are calling the overloaded versions.

Hope this helps.

Charlie
+6  A: 

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.
BalusC
what about an option like C y = new C(); y.a1(); Is that invocation be called as polymorphic?
changed
+3  A: 

I'm a TA for a major American university teaching a Java class, and this is how I always explain polymorphism to my students:

There are actually two things going on here, which are very similar: polymorphism and dynamic binding.

Polymorphism is simply defined as when the reference type and object type are different. The classic example is Animal anim = new Dog(); with Dog extending Animal. The reference type of anim (Animal) is different than its object type (Dog) so anim is a polymorphic reference.

The second part is dynamic binding, which has to do with what method is actually run when you make a method call. Dynamic binding means that the method that actually runs is the method that is farthest down the class hierarchy between the reference type and object type. You can also think of it as the method the belongs to the class closest to the object type.

The class hierarchy is the tree you see where each class is a branch of its parent class. Since Java is single-inheritance (C++ and others allow you to extend multiple classes, but in Java you can only extend one) the class hierarchy is typically a tree. Keep in mind that interfaces and abstract classes are also included in the class hierarchy, so it's not necessarily a tree due to interfaces.

The reason that polymorphism and dynamic binding are used so often together is that polymorphism doesn't make much sense without dynamic binding. Being able to have the method that runs depend on the object type instead of the reference type is the entire point of polymorphism!

Andrew