Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Finally, we have Polymorphism, which means many forms. It's the most complex of the four terms, but very powerful. It lets us automatically do the correct behavior even if what we're working with could take one of many different forms. Well, that sounds a little vague. So, here's an example of polymorphism that you've probably used without even thinking about it: the Plus sign. Well, what does this do? In a lot of languages, it depends. If we're adding two variables together with the plus sign, if these variables are integers, it will numerically add them.
But on the other hand, if the variables A and B are strings, what will happen is that we'll concatenate them. It will automatically do the correct behavior but a different behavior when what we've given it could have one of many different forms. Now, this example is built into a lot of languages, but we can use the same idea with our own classes and our own objects. So, here's an example. If I define a BankAccount class for a financial application and it describes attributes like an accountName and the balance, a deposit method and a withdraw method and even provide some basic behavior for these.
Well, we can then create more specialized subclasses that inherit from it, like a SavingsAccount class, a CheckingAccount class, an InvestmentAccount class. So, they're all inheriting, they all share the basic definition. So they will have an account name, they will have a balance, they can withdraw, they can deposit. But the SavingsAccount might add an interest rate, attribute that CheckingAccount does not have. But then it gets a little bit more complex. So the business rule says that if you withdraw from an investment account, well, you should get a penalty if it's not 30 days notice.
So we have a bit more complex behavior. Now, that withdraw behavior was originally defined in the BankAccount class, so I am already inheriting it. But I can provide a more specialized version of that just for the InvestmentAccount class, and this is referred to as overriding the method of the Superclass, and it just means I write it again. So I am inheriting when useful, but I can override behavior when that's useful. Now, Polymorphism lets us work freely with objects that have been created from any of these classes.
So I could now have an array of accounts with 10,000 of these different objects loaded into it, and I know that I can call the withdraw method on any one of them without knowing exactly what class it was instantiated from, and it would do the correct behavior for each one just as using the Plus sign would automatically switch between addition and concatenation. So it's flexibility. Polymorphism lets us do the right thing at the right time. Now, when learning Object Orientation, Polymorphism is the concept that I hear a lot of people say, "I technically understand it, I just can't immediately think of a place I'd use it. That's okay.
Most of your classes won't need it. We don't have to go looking for polymorphism situations, they occur naturally, as we'll see. And understanding the basic idea is enough for us to move forward.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100058 Viewers
56 Video lessons · 113115 Viewers
71 Video lessons · 81971 Viewers
131 Video lessons · 39327 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.