YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Reusing Methods - Programming Foundations with Python

Get Embed Code
7 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. Okay, so thus far, we've seen how inheritance
  2. can help us with reusing instance variables. Now I
  3. want to show you an example of how inheritance
  4. can help with reusing methods. So here we are
  5. back at the code, where we have a class
  6. called Parent and a class called Child. And if
  7. you recall, class Child inherits from class Parent. Now,
  8. I'll begin by defining a simple instance method inside
  9. class Parent. And I will call it show_info.
  10. The first argument of this method is self, and
  11. all this method does is it prints out the last name and eye color of the parent.
  12. All right, there are the two print statements.
  13. Now to test to see if this method actually
  14. works, I'm going to call this method show_info
  15. using the parent's instance billy_cyrus. So let me do
  16. that next. So there is that method. Now all I've done thus far is created a new
  17. method called show_info inside class Parent. And then
  18. used an instance of class Parent, instance called billy_cyrus
  19. to call that method. Now to be able to focus on this statement's output, I am
  20. going to comment out the other instance statement for now. There. Let
  21. me go ahead and save and run this program. Alright. So the program prints
  22. out the correct values of billy_cyrus' last name and eye color. So far so good.