This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusing regarding Polymorphism..

 
Rahul Hanchinamane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, so am halfway through Head First into Java and ran into this wallbanger of a confusion.

Lets say..



---------------------------------------------------------

How does animList[0].makeNoise() invoke the overridden method in the Dog? From my understanding, the Java compiler relies on whether the reference type has the method to be invoked and hence makeNoise is not invokable on objList[0].

I can even understand that animList[0] will be able to invoke the makeNoise() method from the abstract class. But how does it know to pick the overridden method from the Dog class? There's no way for the compiler to know the object at 0th index is a dog.
Else we could say stuff like:
Dog newDog = animList[0];
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul. Welcome to The Ranch!

It works because polymorphism happens at run-time. The compiler knows it's safe to call makeNoise() on any Animal object, because the method is defined in the superclass. It only needs the reference type to be able to work that out. But the determination of exactly which version of makeNoise() gets called is done at run-time, because on the actual type of the object.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Hanchinamane wrote:
How does animList[0].makeNoise() invoke the overridden method in the Dog? From my understanding, the Java compiler relies on whether the reference type has the method to be invoked and hence makeNoise is not invokable on objList[0].



Correct. The compiler uses the reference type to determine whether a method (with the correct signature) exists -- and generates the call to it. So, if the method doesn't exist in the reference interface, it won't allow the method to be called.

As for how polymorphism works, once it is deemed that the called is allowed, the compiler generates code to call the method via a jump table (at least with the Sun implementation of java). It doesn't call the method directly. It generates a call that looks it up to call it -- this is of course, done at runtime, and causes the call to the correct method (the method based on the type of the instance).

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And BTW....



You never instantiated the elements of the array, so this code will throw null pointer exceptions.

Henry
 
Rahul Hanchinamane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Matthew Brown @Henry Wong

Thanks a ton for the quick replies and easing my headache. I think I understood the concept this time.

Henry Wong wrote:

You never instantiated the elements of the array, so this code will throw null pointer exceptions.

Henry


It was just a rough code to illustrate the problem. Not using that code IRL.
 
Magnus Spaang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Hanchinamane wrote:I can even understand that animList[0] will be able to invoke the makeNoise() method from the abstract class. But how does it know to pick the overridden method from the Dog class? There's no way for the compiler to know the object at 0th index is a dog.
Else we could say stuff like:
Dog newDog = animList[0];


You almost can.
Dog newDog = (Dog)animList[0];
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic