Return to Video

Polymorphism - Intro to Java Programming

  • 0:00 - 0:03
    Now Sarah has shown you another example of a interface, and I hope you've
  • 0:03 - 0:07
    become more familiar with how they work. They're really pretty natural. But
  • 0:07 - 0:11
    when you think about it, there is something mysterious going on. In our
  • 0:11 - 0:17
    suburban scene, we had an array list of drawables. Houses, cars, dogs and so
  • 0:17 - 0:21
    on. We got one of them, store it in a variable of type drawable, of course,
  • 0:21 - 0:26
    then call the draw method on it. That our stored
  • 0:26 - 0:31
    d belongs to the drawable type, and drawable has the draw method. What's d?
  • 0:31 - 0:36
    It's the variable, so it holds a reference to an object. An object of what
  • 0:36 - 0:41
    class? You might think it's an object of class drawable. Now wait a minute,
  • 0:41 - 0:46
    there is no class drawable. Drawable is an interface. So that can't be it, and
  • 0:46 - 0:52
    in fact, there is no way of knowing to which class this object belongs. There
  • 0:52 - 0:57
    is only one thing that we know about it, this object belongs to some class that
  • 0:57 - 1:03
    has a draw method. And in fact as you loop over the various elements in the
  • 1:03 - 1:09
    array list, this line of code may call different methods. The draw method of
  • 1:09 - 1:12
    the house class, the dog class, or of some other class, so far unimagined, that
  • 1:12 - 1:19
    also chooses to implement the drawable interface. This variation is called
  • 1:19 - 1:24
    polymorphism, which is just a fancy word for saying different shapes. In our
  • 1:24 - 1:29
    scene, that's a very appropriate name. Because the draw method can draw
  • 1:29 - 1:34
    entirely different shapes depending on what the implementing class does. But
  • 1:34 - 1:38
    the term polymorphism is used generally in Java to indicate any situation where
  • 1:38 - 1:42
    you have a method call and the actual method called depends on the type of the
  • 1:42 - 1:50
    object. Now, why is polymorphism important? It lets us build expandable
  • 1:50 - 1:54
    systems, where we can add new types without having to change the essential
  • 1:54 - 2:00
    logic of the program. I'd like you to try this out and add a new type to our
  • 2:00 - 2:07
    suburban scene. Namely a ball class and simply the kind of ball you may find
  • 2:07 - 2:12
    lying on the street. And when you do that, note how little of the program you
  • 2:12 - 2:13
    have to change.
Title:
Polymorphism - Intro to Java Programming
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
cs046: Intro to Programming
Duration:
02:15
Dejan Trajkovic edited English subtitles for 21-13 Polymorphism
Udacity Robot edited English subtitles for 21-13 Polymorphism
Udacity Robot edited English subtitles for 21-13 Polymorphism
Udacity Robot edited English subtitles for 21-13 Polymorphism
Cogi-Admin edited English subtitles for 21-13 Polymorphism

English subtitles

Revisions Compare revisions