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.
00:11 And also, like, that it’s nice to have fun with these types of implementations. And it’s a good training to just think out real-life concepts and real-life objects in code.
00:22
It’s going to help you to train the concepts of object-oriented programming. We’ve done .move()
and also used this new attribute in here that points to a location object.
00:34
We used aggregation to aggregate Animal
objects in FarmLocation
objects. We’ve used a ton of inheritance. Now here’s a little squiggly line that I left over to look at .fetch()
.
00:47
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 Pig
nor 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.
01:03
Okay, when I head back over to the code, I’m going to go to the child class Dog
01:13
and additionally to .talk()
, I’m just going to implement a little additional instance method that I’m going to call .fetch()
, and it’ll have access to self
and a thing
.
01:27
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 "{self.name}
01:44
dashes after the {thing}..."
, again in curly braces. That’s going to be a string, whatever you pass in, whatever you’re throwing for the dog to fetch.
01:54
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.
02:16 And then print another message.
02:22
f"{self.name} returns
the {thing} to you"
. Such a good dog.
02:33
And I will return, not thing
here, I’m going to return the thing
. And this I wanted to talk about a little bit too, that in this model of the farm, you’re really just returning information.
02:45
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.
03:23
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.
03:56
But with that done, we have a little .fetch()
method on the Dog
class, and let’s play around with this a bit in the next lesson.
Become a Member to join the conversation.