• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Tricky question about polymorphism

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've encounter the following suprising behaviuor with polymorphism and I need someone to explain it to me.

This is simple example :

abstract Class A
{
void test(A a)
{
System.out.println("You are in A");
}
}

Class B extends A
{
void test(B b)
{
System.out.println("You are in B");
}
}



A b1 = new B();
A b2 = new B();
b1.test(b1);

result:

You are in A
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Doni,

Welcome to JavaRanch!

The short answer is that there's no polymorphism here. The two different "test" methods have different argument lists, so they are not polymorphic; they're said to be overloaded. It's up to the compiler to decide which of a set of overloaded methods is called, and the compiler looks only at the type of the variable on which the method is called. In contrast, the choice among polymorphic methods is made at runtime based on the actual type of the object.

In any event, here, the compiler sees only that the variable is of type "A", and it looks for a method "test" in "A" that it can call with an argment of type "B", and finds one, and calls it.

Now, if you change the argument type of B.test() to be "A", then you'd see polymorphic behavior. Try it!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


// Because the test in myA should only accept (new A()'s)
// Instances of the A class should be the only objects
// that could enter the A.test(A a) method



But note that class B extends class A, and therefore, by definition, a B is an A. An instance of B can correctly be passed to either A.test(A) or B.test(B); an instance of A could only be passed to A.test(A).
[ July 22, 2004: Message edited by: Ernest Friedman-Hill ]
 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now, thank you
[ July 22, 2004: Message edited by: Corey Hollaway ]
 
doni klauz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!