• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please clarify my understanding of Polymorphism

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on Sierra/Bates, Chapter 2, Question 8



This code compiles. When it runs, it outputs:

howl howl sniff

From what I understand

((Dog) new Hound()).bark(); <- at compile-time, compiler will check to see if Dog has bark() method
((Dog) new Hound()).bark(); <- at run-time, jvm will look at actual class it is casting, i.e. Hound, and will execute Hound's bark() method.

Please explain why Java 6 is designed this way? At first glance, one would think that jvm would execute Dog's bark() method.

How does this feature of Polymorphism facilitate real-world applications?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't designed in Java 6, it exists before Java 6. It's called Virtual Method Invocation. Search Google, You can find real - world examples.

Your understanding is correct!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most simple link I found was here

http://msdn.microsoft.com/en-us/library/aa645767(v=VS.71).aspx


Even though it is not in Java, you can glean an understanding from it.


Please critique my example of how Virtual Method Invocation may be used in real-world applications

You have a Facebook application where DOG does the following: eat(), sleep(), play(), walk(), run(), rollOver(), and (of course) bark()

So, application is a drop-down which holds type DOG, and the only entries in the drop-down are objects of type DOG, i.e. HOUND, POODLE, BEAGLE, LABORADOR.

You cannot have the drop-down perform, say HOUND.biteMailman(), because that method is not part of DOG (at least, not a well-trained DOG ;-)) . In other words, compiler ensures that all of DOG's children (i.e. HOUND, POODLE, BEAGLE, LABORADOR) can perform the consistent actions as DOG. However during run-time, the children perform DOG's action according to their own implementation (after all, each breed of DOG has a distinct way of doing things)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a succinct response (Watch out for the typos in the explanation):

http://wiki.answers.com/Q/What_is_virtual_method_invocation_in_java

And here is the one-liner:

http://wiki.answers.com/Q/What_is_virtual_method_invocation_in_java_and_net

Basically, it is the difference between compile-time and runtime types.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Cholakis wrote: Here is a succinct response (Watch out for the typos in the explanation):

http://wiki.answers.com/Q/What_is_virtual_method_invocation_in_java

And here is the one-liner:

http://wiki.answers.com/Q/What_is_virtual_method_invocation_in_java_and_net

Basically, it is the difference between compile-time and runtime types.



Hi Alex,

Thanks for those two links, particularly the first one. At first, one may think the output would be blue2, but now, polymorphism refers to the execution of methods in the class, not their local variables.
reply
    Bookmark Topic Watch Topic
  • New Topic