English subtitles

← The Right Display Method Is Called - Intro to Java Programming

Get Embed Code
7 Languages

Showing Revision 8 created 09/10/2017 by Dejan Trajkovic.

  1. We've just talked about which display method gets called in the present question
  2. method. The method has a parameter whose type is question. So here's the
  3. parameter variable. It contains an object reference which points to an object,
  4. an object of what type? Well you think it's question
  5. because that, after all, has the type of q. But not so fast.
  6. The type of q, the variable is indeed question. The type of the object is really not known. It could be any sub-type
  7. of question. Now the good thing is, that when the program runs it does the
  8. right thing. At runtime, it's determined what the exact type of this object is.
  9. And its display method is caught. That might be question.display, choice
  10. question display, or the display method of some other class. That also extends
  11. the question class, this is a very useful feature because it allows us to write
  12. generic codes such as present question that works with questions of any type
  13. and actually this diagram should be familiar to you, you've seen the same
  14. diagram when we talked about interfaces and there is the name for the fact that
  15. the correct version of a method gets selected at run time. Remember what that's
  16. called? Was it object-oriented programming? Encapsulation? Inheritance? Or
  17. Polymorphism?