Extend Child Class With Extra Method
00:00 Now this final piece of logic that I’m still going to introduce into this model of a farm is going to be a fun one where I just want to point you to the direction that there’s a lot more that you can do.
So we haven’t implemented any custom instance methods on any of the subclasses, so I want to do a little
.fetch() method for
Dog objects that neither
Sheep have and play around with this a bit more and also show you that you can continue building on this farm model as much as you want to.
So here I’m going to again pass in an object that the dog’s going to run to fetch. Let me just give a bit of output. I’m going to print a message. I’m going to make it an f-string again for
And then I’ll make you wait for the dog to come back. So let’s import
time here at the top of the script. I’m going to say
import time and then use it inside of the
.fetch() method to sleep for a second.
time.sleep() for let’s say half a second. Long enough. It’s a very fast dog.
Basically, you’re returning logging information of what’s going on in your classes when you’re working with the instances. And most of the time, when you’re modeling something in OOP, it’s not going to return a string of what just happened. But instead, you’re probably going to be returning attributes or values or other objects in order to then continue working with them. In case of this
.fetch() method, for example,
thing could be yet another class of objects, throwable objects, let’s say, or objects that a dog is interested in fetching. Wow, that would be a great class name.
And you could just have this little simulation of a dog fetching. And then in the end you would get back whatever
thing you passed in. And then you could, this
.fetch() method could pass the
thing on to another method that does something else with it. I don’t know what exactly, and maybe my abstraction ideas end at this point, but I just wanted to also point that out that most of the time when you’re working with classes, you’re not going to be returning strings that give you information, but you’d rather be returning code objects that you can continue working with.
Become a Member to join the conversation.