🎙 Develpreneur Podcast Episode

Audio + transcript

Polymorphism

This episode is about polymorphism, a powerful concept in object-oriented programming that allows for a common interface across different objects.

2021-02-20 •Season 14 • Episode 466 •Polymorphism •Podcast

Summary

This episode is about polymorphism, a powerful concept in object-oriented programming that allows for a common interface across different objects.

Detailed Notes

Polymorphism is the ability to make the same call or do the same thing across different objects and have different results. It's a fundamental concept in object-oriented programming that allows for a common interface across different objects. The host provides examples of polymorphism in action, such as save and load operations. Polymorphism is not just about user experience, it's also about developer experience. It gives us a common language to use across our objects. The host mentions that this is a long series of episodes on practical approach to object-oriented programming.

Highlights

  • Polymorphism is the ability to make the same call or do the same thing across different objects and have different results.
  • It's like plugging a cartridge into a console, the console still works the same way.
  • Polymorphism gives us a common language to use across our objects.
  • Save is a great example of polymorphism, it's a universal concept that can be applied to different objects.
  • Polymorphism is not just about user experience, it's also about developer experience.

Key Takeaways

  • Polymorphism is a powerful concept in object-oriented programming.
  • It allows for a common interface across different objects.
  • It's not just about user experience, it's also about developer experience.
  • Polymorphism gives us a common language to use across our objects.
  • It's a fundamental concept in object-oriented programming.

Practical Lessons

  • Use polymorphism to create a common interface across different objects.
  • Implement polymorphism in your code to improve developer experience.
  • Polymorphism can be used to improve user experience as well.

Strong Lines

  • Polymorphism is the ability to make the same call or do the same thing across different objects and have different results.
  • It's like plugging a cartridge into a console, the console still works the same way.
  • Polymorphism gives us a common language to use across our objects.

Blog Post Angles

  • The power of polymorphism in object-oriented programming
  • How polymorphism improves developer experience
  • Real-life examples of polymorphism in action
  • The benefits of using polymorphism in code
  • How polymorphism can improve user experience

Keywords

  • polymorphism
  • object-oriented programming
  • common interface
  • developer experience
  • user experience
Transcript Text
This is Building Better Developers, the Develop-a-Noor podcast. We will accomplish our goals through sharing experience, improving tech skills, increasing business knowledge, and embracing life. Let's dive into the next episode. Well, hello and welcome back. We are continuing our season where we're looking at a practical approach to object-oriented programming and taking some of these concepts that are now really baked into a lot of what we do and finding some better ways to expand upon them and make sure that we're actually still incorporating them into the code we're creating, not just the code that is provided for us via framework or some sort of generator. This episode, we're going to start into what's going to be probably a long series within this season of episodes, we're going to talk about polymorphism. This is something that appears all over in development and software development. It is a very powerful aspect of it. In particular, we'll find that it gets into essentially it's not quite user experience in the UX case. It's more user experience for the developers that are using code or framework or library. Put simply, polymorphism is the ability to make the same call or do the same thing across different objects and have different results. These objects understand but do it differently. This is pervasive in everything we do. If you, let's start with just applications itself. Most applications that are windowed have a menu. A menu alone is and it's typically going to be up at the top of the application, depending on where you're at and what you're doing, but that's more or less in general. Within that menu, there's going to be very common commands that you can essentially give to the application, which is by clicking on that menu item. Save for example. Whatever your application is, there's probably some sort of a file save or something like that that is an option. While it may be a very different file that's done, and the save option itself may actually be very different when it does. You may not actually be saving a file. There may be a save that instead is storing information back in a database or something along those lines. However, that is polymorphism. We just say save and we click on the save and the application saves data. We have that understanding, so we're able to share this shared concept so we don't have to relearn what's going on or a command from, in this case, application to application to application or object to object to object. Now, save is a great example because there are, across most objects, if they have some sort of a backing store or some way to serialize them and store it or something like that, there is a concept of save. Now, that may save it in a database, may save it to a file, may just write it to some session memory, whatever it is, but a lot of objects you can do object.save and it's going to store that information. The idea being that we store it and later we can do an object.load and pull that information back into an instance. Those are primary examples. Those are very key for usability and it's good for that user experience or usability of the code we write. If in one case we write save, in another one we have a function called store, in another one we call it, I don't know, cache, and another one we call it complete, and another one we call it done. It becomes really complicated really fast to use our code when we do that. So polymorphism allows us to have a consistent, essentially, in a sense, look and feel to our classes. It gives us a foundation of things we can go to as part of experiencing and using classes and instances that we have created. This goes throughout our objects, our classes, and then our instances. It's not just save and load. There are going to be things like attribute names, essentially, like IDs. If you look for an ID, it's probably called ID or maybe class name ID, but typically you have an ID available. This becomes beyond the user experience. This becomes critical really when we start moving into the ideas of inheritance and interfaces and things like that. You could almost think of it, and there's a lot of ways to think of this kind of stuff, but one of the things you can think of is if you go back to the old console games. We'll talk about this again when we talk about interfaces. You had these little whatever they were, whatever shapes they took, they were essentially cartridges that you would plug into the console, and then you would have your game, whatever that game was. It was a very simple interface, but that was the power of the object-orientedness of it. It's not really that, but that's polymorphism, is it's saying, hey, I can plug this cartridge in and I can also come back and plug a different cartridge in. The console still essentially works the same way. I'm connected to my joystick or game controller or whatever it is, and it displays and stuff like that, but that's all because of the polymorphism piece of it, is having a standard way to do an action. It seems very generic, but that's really the goal of it. That's one of the things that we want to build is where possible, we want to have within our objects ways that are, I guess we'll call them universal. A save is a great example, but also a display or print would be another one. Let's say I've got an application and I've been running it for a while, I've got all kinds of objects that are, or instances actually, that are in memory. Let me back up a second, because I don't think I've really covered that, but just from a terminology point of view, a class is the description of a class, of what it does and what it includes. When I talk about a class, when I talk about an instance, that is where I've actually taken that class and I've created it in memory. If I look at the code for a customer class, then I can see that it's going to have an ID and a name and an address and things like that. An instance of a customer is a specific. It's John Doe that lives at 123 Main Street and has the phone number 555-555-1234, something like that. So an instance is actually that data, whereas the class is the definition of it. So back to this, if you think about printing and saving and common things that you need to do, it's nice to have that common interface because you've got all these instances of your application and somebody does, clicks on the menu and says file save and they want to save the state of that application. The easiest way for you to do it from a developer point of view would be take the collection of all of the objects that you've got in memory and just start walking through them and telling them to save themselves because you may have very different ways that all these things save themselves. And so rather than having to specifically maybe go through the things that saved a file and separately go through the things that saved a database and separately go to the things that have to get sent to some website somewhere or save through an API, you don't want to have all these like ifs and else's and checking for types and all that kind of stuff built into your code. From a developer's point of view, you just want to say, here's all my instances, save them. And that's essentially what polymorphism allows us to do is because when you say save on an object, that object through polymorphism understands that save command, basically that save function and says, okay, they want me to save. They have this concept of saving they want me to do. And so I'm going to go do that. Now as a developer of that class behind the scenes, you've built, you've written code that supports that concept, that supports the idea of save. It gives us a common language with which to interact with our applications. So polymorphism is very powerful there. And again, it's something we see in the real world all the time. So why it may seem like it's a, I don't know, like a complicated word or concept. It really isn't. You know, if you say to, and like I said, we actually in real life do this to very interesting degrees. If you've got a family and you're going to go out to eat dinner and say you've got five or six people in your family and you say, get ready, we're going to go for dinner. Every one of those people has a different thing that they do or series of things they do to get ready. Some may just put on some shoes. Some may go get a shower and get cleaned up and have makeup to apply and stuff like that. Others may go grab things to do while they're, wherever you're going. Like if you've got kids, maybe they go and grab a, that means, oh, get ready means I need to grab my phone or my portable device that I'm going to entertain myself or a book or something like that. But that get ready would be the polymorphism that now every person understands that in a different way. So it really is, it becomes polymorphism gives us our, really our common language to use across our objects. So like I said, this is where we're going to, we're going to talk about this quite a bit in different ways that we, that we can use this, that we can implement this and that we can provide this to others and how it really should be a very essential part of our design considerations and thoughts. And what we do is we actually start working into implementing that design. That being said, we'll pause here for now until we come back the next time around. Challenge of the Week is get your, just spend a couple minutes and think of a half dozen or a dozen common commands or functions or even ID names that would essentially be polymorphism that would allow us from one object to another to still have the same backing concept to it. Because I think if you have these in your mind as we're moving forward, it will help, it help you give some concrete examples to some of the things we're going to talk about along with the ones that we're going to bring up as we go. But that being said, I will let you go. I'll let you go get ready for your day, whatever, however, wherever that makes, that actually becomes a reality for you. But as always, get another polymorphic command, go out there and have yourself a great day, a great week, and we will talk to you next time. Thank you for listening to Building Better Developers, the Developer Noor podcast. For more episodes like this one, you can find us on Apple Podcasts, Stitcher, Amazon, and other podcast venues or visit our site at developernoor.com. Just a step forward a day is still progress, so let's keep moving forward together. There are two things I want to mention to help you get a little further along in your embracing of the content of Developer Noor. One is the book, The Source Code of Happiness. You can find links to it on our page out on the Developer Noor site. You can also find it on Amazon, search for Rob Brodhead or Source Code of Happiness. You can get it on Kindle. If you're an Amazon Prime member, you can read it free. A lot of good information there. That'll be a lot easier than trying to dig through all of our past blog posts. The other thing is our mastermind slash mentor group. We meet roughly every other week and this is an opportunity to meet with some other people from a lot of different areas of IT. We have a presentation every time. We talk about some cool tools and features and things that we've come across, things that we've learned, things that you can use to advance your career today. Just shoot us an email at info at developernoor.com if you would like more information. Now go out there and have yourself a great one.