Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
What does this program show? Say it before you run it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. talk is written once, in Animal. It calls self.speak(), and Python picks the speak of the object's own class.
Animal(): speak is the parent's, "...". So I say ....Dog(): speak is the dog's, "Woof". So I say Woof.Cow(): Cow has no speak, so Python goes up to Animal. I say ....Check. The method talk is not overridden anywhere, but its answer differs, because the call inside it goes to the right speak for each object. This is polymorphism.
Answer: I say ..., I say Woof, I say ....
Log in to join the discussion.
Loading…