From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Polymorphism: Example - Python Tutorial
From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach
Polymorphism: Example
- [Instructor] We are now going to code a very simple example of polymorphism, so let's get started. Class ClassA. This is going to have a method, my_lovely_method. And it takes self as an argument. And what it's going to do is to print my_lovely_method. And it's going to tell you where it comes from. So it's from ClassA, okay? So it's a very simple class we have there. Now, let's have another class, which inherits from ClassA. So class ClassB, and this is going to inherit from ClassA. So that means in principle, it should have the same methods. However, we're going to override my_lovely_method. So we're going to do def my_lovely_method. (giggles) I did auto complete there on VS Code, and it did far more than I wanted it to. So I'll leave that in there just so you can see that that's a possibility. Sometimes doing auto complete gives you some code that may or may not be helpful. But in this, instance I want to do it myself. So I'm going to print my_lovely_method, but this is from…