Return to Video

Object Inheritance (Video Version)

  • 0:01 - 0:03
    We're back with our program
    that creates Winstons
  • 0:03 - 0:06
    but I've added a new object type, Hopper,
  • 0:06 - 0:09
    because Hopper was feeling
    a little left out.
  • 0:09 - 0:12
    Now I define Hopper
    the same way that I define Winston.
  • 0:12 - 0:15
    Starting off with the constructer function
    and taking the same properties
  • 0:15 - 0:21
    and then draw, and talk, and then
    I also added another method called Horray
  • 0:21 - 0:25
    because Hoppers really like to celebrate
    and Winstons don't really at all.
  • 0:25 - 0:30
    Now at the bottom of the function,
    I've created two new Hopper objects:
  • 0:30 - 0:32
    Little Hopper and Big Hopper
  • 0:32 - 0:37
    and drawn them and called talk on one
    and Horray on the other.
  • 0:37 - 0:38
    So that's pretty neat
  • 0:38 - 0:43
    Now, if we look at this code up here,
    you might notice something interesting.
  • 0:43 - 0:47
    The code for hopper is very similar
    to the code for winston.
  • 0:47 - 0:51
    Particularly look at this constructor.
    I don't know if you remember but that is
  • 0:51 - 0:55
    exactly the same code
    as our Winston constructor function.
  • 0:55 - 0:58
    And then this talk function, is also
    exactly the same code
  • 0:58 - 1:04
    as our Winston talk function,
    and they also both have draw functions.
  • 1:04 - 1:07
    So there's a lot of things in common
    about these two object types
  • 1:07 - 1:10
    and that makes sense because
    Hopper and Winston
  • 1:10 - 1:13
    they're two very similar object types
    in our world.
  • 1:13 - 1:18
    If you think about the real world,
    outside the computer,
  • 1:18 - 1:21
    most object types shares similarities
    with other object types.
  • 1:21 - 1:26
    Like in the animal kingdom.
    All animals are similar in some ways.
  • 1:26 - 1:30
    And then we have different types
    of animals, like humans.
  • 1:30 - 1:34
    Humans share those similarities but also
    have their own unique similarities.
  • 1:34 - 1:38
    So we could say that animal
    is an object type
  • 1:38 - 1:42
    that human object types
    inherit functionality from.
  • 1:42 - 1:44
    We don't completely start from scratch.
  • 1:44 - 1:47
    We add on top of the functionality
    that we get from being an animal.
  • 1:47 - 1:52
    Like all animals make noises,
    but humans also make language.
  • 1:52 - 1:56
    So this concept of object inheritance
    is really useful in programming too.
  • 1:56 - 2:01
    And we can create a chain of
    object inheritance in our Javascript.
  • 2:01 - 2:05
    So to do this when you think about
    what's shared about our object types.
  • 2:05 - 2:07
    And come up with a name for that
  • 2:07 - 2:10
    'cause we're going to create
    a new object type that represents
  • 2:10 - 2:12
    the base object.
    So let's call them creatures.
  • 2:12 - 2:14
    They're both creatures.
  • 2:14 - 2:16
    So we say var creature equals...
  • 2:16 - 2:20
    And now we need our constructer function
    So let's go and just steal Hopper's
  • 2:20 - 2:24
    since that's the same thing
    that Winston has. Alright.
  • 2:24 - 2:29
    And then... let's see. Now we want to...
    What do we want to do next?
  • 2:29 - 2:33
    Maybe we want to add
    the "talk" function on it.
  • 2:33 - 2:36
    Talk function, we could just
    steal Hoppper's.
  • 2:36 - 2:40
    But of course we need to have it on
    the creature's prototype instead.
  • 2:40 - 2:46
    Okay, cool. So now we have
    this creature object type.
  • 2:46 - 2:48
    But we need to actually tell Hopper
  • 2:48 - 2:53
    that Hopper should actually
    base it's functionality off of creature.
  • 2:53 - 2:56
    So we can do that
    by writing this line here.
  • 2:56 - 3:05
    We'll say "Hopper.prototype
    equals object.create, creature.prototype"
  • 3:05 - 3:10
    So what this line does, tells Javascript
    to base Hopper's prototype,
  • 3:10 - 3:16
    so to base all of Hopper's functionality,
    off of the creature prototype.
  • 3:16 - 3:20
    That means that every time it looks
    for a function on a Hopper,
  • 3:20 - 3:24
    it'll look at Hopper's prototype first,
    but then if it doesn't find that
  • 3:24 - 3:27
    it'll actually look and see
    if it was on the creature prototype.
  • 3:27 - 3:30
    And this is what we call
    the prototype chain.
  • 3:30 - 3:32
    Now, once we've done this
    we should actually be able to
  • 3:32 - 3:38
    delete the talk function on Hopper
    because it already exists on creature.
  • 3:38 - 3:42
    Which is up the prototype chain,
    so let's try it. Ready? ♪Dun dun dun♪
  • 3:42 - 3:48
    It worked! And it works because
    it finds it on creature prototype instead.
  • 3:49 - 3:52
    So let's try deleting it on Winston too.
  • 3:53 - 3:58
    Okay. It didn't work it says
    "object has no method 'talk'".
  • 3:58 - 4:01
    And why is that?
    Well we have our Winston constructer
  • 4:01 - 4:03
    and draw and we took away the talk.
  • 4:03 - 4:07
    Well, you notice we forgot to actually
    tell Winson's prototype
  • 4:07 - 4:09
    to be based off the creature prototype.
  • 4:09 - 4:14
    So we need that very important line.
    Winston.prototype=object.create
  • 4:14 - 4:17
    (Creature.prototype)
  • 4:18 - 4:20
    Ta Da!
    And notice something important.
  • 4:20 - 4:23
    I have this line after
    the constructor function
  • 4:23 - 4:27
    but before I add anything else
    to the Winston prototype.
  • 4:27 - 4:30
    So that's usually what you want to do.
    You want to tell it
  • 4:30 - 4:32
    just as your starting off immediately:
  • 4:32 - 4:34
    what your initial prototype
    will be based on.
  • 4:34 - 4:37
    But then we keep adding more stuff
    to it's prototype.
  • 4:37 - 4:39
    Because there could be some things
  • 4:39 - 4:42
    that are unique to Winstons
    or unique to Hoppers
  • 4:42 - 4:43
    that aren't on creatures.
  • 4:43 - 4:46
    And that's totally cool
    that you can define those.
  • 4:46 - 4:50
    Alright. Now, if we look at this
    we still have some repeated code.
  • 4:50 - 4:52
    The constructor code.
  • 4:52 - 4:54
    Right? We have this all three times.
  • 4:54 - 4:59
    So could we just delete it?
    Let's try it.
  • 5:01 - 5:04
    Okay. Hmm...
    Doesn't seem like that worked
  • 5:04 - 5:06
    'Cause our Hopper shows up
    in the upper left corner.
  • 5:06 - 5:08
    It kind of forgot everything about itself.
  • 5:08 - 5:11
    And this is because Javascript
    does not assume you want
  • 5:11 - 5:15
    the same constructor even if you want
    to base the prototype off of it.
  • 5:15 - 5:19
    It lets you define your own
    constructor for these objects.
  • 5:19 - 5:26
    But it also does give you an easy way
    to call a constructor from a sub-object
  • 5:26 - 5:28
    The way we can do this is,
    we will write
  • 5:28 - 5:36
    creature.call(this,nickname,age,x,y)
  • 5:36 - 5:41
    Now what this does--notice it worked. yay.
    And what it did actually
  • 5:41 - 5:44
    it's calling the creature function,
    --the construction function.
  • 5:44 - 5:47
    It's calling that function
    but it's passing and saying,
  • 5:47 - 5:50
    okay you should call this
    constructor function as if
  • 5:50 - 5:54
    it was being called from
    this Hopper object
  • 5:54 - 5:57
    and as if it's being called with
    these arguments.
  • 5:57 - 5:59
    These are arguments that
    the Hopper got called with.
  • 5:59 - 6:04
    And that will end up just executing
    this code as if it was right here.
  • 6:04 - 6:07
    And that's exactly what we want.
    And it worked.
  • 6:07 - 6:09
    And we can go ahead and
  • 6:09 - 6:15
    copy this line into
    the Winston constructor too.
  • 6:15 - 6:17
    And it works. Yay!
  • 6:17 - 6:20
    Alright. So check this out.
    We've encapsulated all our shared
  • 6:20 - 6:23
    properties and functionalities
    about objects in this single base
  • 6:23 - 6:25
    object type: creature
  • 6:25 - 6:28
    And we've made two object types
    that extend from that base object.
  • 6:28 - 6:32
    They inherit some functionality
    but they add their own too.
  • 6:32 - 6:33
    And the cool thing about this
  • 6:33 - 6:36
    is that we can change the shared
    functionality in a single place.
  • 6:36 - 6:41
    Like if we wanted to change age again.
    We could say "+ yrs old".
  • 6:41 - 6:44
    Cool, now everybody has
    "yrs old" at the end of it.
  • 6:44 - 6:49
    Or we could change the "talk" functions
    and be like "suppp?!". Woo!
  • 6:49 - 6:53
    And now both Winstons and Hoppers
    are saying "sup".
  • 6:53 - 6:54
    So now that you've seen how to
  • 6:54 - 6:57
    create object types
    and inherit from object types,
  • 6:57 - 6:59
    you can start thinking
    about how this might be useful
  • 6:59 - 7:02
    in your drawings and
    animations and simulations and games.
  • 7:02 - 7:05
    For example, maybe you have a game and
    you have many types of characters in it
  • 7:05 - 7:08
    All of them can run
    but only some of them can jump.
  • 7:08 - 7:12
    That's a perfect place for some
    object types and some inheritance.
  • 7:12 - 7:16
    But I bet you can think of
    tons of more ways as well.
Title:
Object Inheritance (Video Version)
Description:

This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/

more » « less
Video Language:
English
Duration:
07:17

English subtitles

Revisions