This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confusing regarding Polymorphism.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusing regarding Polymorphism.." Watch "Confusing regarding Polymorphism.." New topic
Author

Confusing regarding Polymorphism..

Rahul Hanchinamane
Greenhorn

Joined: May 23, 2011
Posts: 2
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

And BTW....



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

Henry
Rahul Hanchinamane
Greenhorn

Joined: May 23, 2011
Posts: 2
@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

Joined: Jun 19, 2011
Posts: 2
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];
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusing regarding Polymorphism..
 
Similar Threads
Do I make it wrong?
Abstract Methods and Classes
Upcasting.....Its confusing
polymorphism
Abstract and interface