Course 1.2Lesson 2 of 5
A child class can override a method of the parent by writing a method with the same name, and Python uses the child's version for the child's objects. Objects of different classes can answer the same call in their own way, which is called polymorphism, so code can treat a list of mixed objects alike. A child can also extend a method by calling the parent's version with super and adding to it. A parent can promise a method that every child must write, by raising NotImplementedError, and a method of the parent can call that promised method and so work for every child.
Try the code and the simulations beside the lesson.
After this lesson you can
super(), andA child gets all the methods of its parent. But sometimes the parent's version is not right for the child. A dog does not make "a sound". It says Woof. The child can override the method: write a method with the same name. Python then uses the child's version for the child's objects.
Predict first. What does each of the four animals say? Then run it.
Dog and Cat each write their own speak. For a dog, Python finds speak in Dog first, and uses it.Fish writes no speak, so Python goes up to Animal, and uses the parent's.Animal("Thing") uses Animal.speak.The loop at the end is the important part. The same call, animal.speak(), gives a different answer for each kind of object. The loop does not ask "is it a dog or a cat?". It just calls speak, and each object does the right thing. This is called polymorphism, from Greek words for "many forms". It lets you write code that works for any animal, even one that does not exist yet. When someone adds a Bird class with its own speak, the loop works for birds without any change.
A child often wants to do what the parent does, and more. Then it calls the parent's version with super().
Notice that the overriding method here replaces the rule completely. If you wanted the parent's checks plus something more, you would call super().withdraw(amount) inside it.
The loop never asks what kind of shape it holds. Adding a new kind of shape needs a new class, not a change to the loop.
Sometimes the parent cannot know how to do a job, but it knows that every child must. A Shape cannot find an area, but every Square and Circle can. The parent promises the method, and refuses to run it.
This pattern, a parent with a general method that calls a part that the children fill in, is used everywhere in real programs: in games (every enemy has an attack), in graphics (every shape has a draw), and in frameworks.
super() when the child should do the parent's part too.if type(x) == Dog to choose what to do. Let each class do its own job, and call the same method.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
super().method(...) calls the parent's version, to extend it.NotImplementedError, and its own methods can call it.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
speak runs for Fish("Nemo") when Fish has none of its own?NotImplementedError in a parent mean?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.