Programming fundamentals›Objects that share behaviour
Tutorial 12 of 13 · Programming fundamentals · Programs that cope · BeginnerObjects that share behaviour
Let one kind of object borrow what another already does, and change only what differs.
5 lessons, the last is a challenge · about 1 h · Python and Java
Before you startYou finished "Classes and objects": you can write a class with a constructor, fields and methods, and make objects from it.
What you’ll be able to do
- 1Build a class on another with inheritance, reuse the parent's constructor with super, and override a method the child does differently.
- 2Keep objects of several classes in one list, call one method on each, and say which class's version runs for every item.
- 3Choose between "is a" (inheritance) and "has a" (composition), and say what a Java interface promises and what Python needs instead.
Lessons
- 1Same action, different thingsGive two classes a method with the same name, and say which one runs for each object.10 min, up next
- 2A class built on anotherBuild a child class on a parent, reuse the parent's constructor, and replace one method.14 min
- 3One loop, many kindsLoop over a list of mixed shapes, call one method on each, and read which version ran in the trace.13 min
- 4Interfaces, and when to stopSay what a Java interface promises, what Python needs instead, and when a field is better than a parent.13 min
- 5Final challenge: a small family of classesFinal challenge · Predict, complete and explain programs built from a parent class and its children.10 min
Next in this path
Next: 13 · Build a small program · 6 lessons · 1 h 15 min
Put every piece together into one small program that reads, decides, repeats and saves its work.
Playground
Try the same model with your own values. No checks, nothing recorded.
Open the playground