Course 1.2Lesson 4 of 5
Two ways to build a class from other classes are inheritance, which says is a, and composition, which says has a, where an object keeps other objects in its attributes. A Dog is an Animal, but a Car is not an Engine, it has an Engine. Composition is more flexible, because the parts can change, and it avoids long family trees. A Garage that has a list of Cars, and a Car that has an Engine, are composition. A rule of thumb is to prefer composition, and to use inheritance only for a clear is a. When a design feels hard, draw the objects and the arrows and read each arrow as is a or has a.
Try the code and the simulations beside the lesson.
After this lesson you can
You now know one way to build a class from another: inheritance. There is a second, and it is used even more often: composition. An object simply holds other objects in its attributes.
Ask one question, and say the sentence aloud.
Dog is an Animal. Say it: it sounds right. Use inheritance.Car has an Engine. A car is not an engine. Use composition: the car keeps an engine in an attribute.Predict first. Which cars have a running engine after the first car drives? Then run it.
Car.__init__ makes an Engine and keeps it: self.engine = Engine(power). The car has an engine. A car does not have to know how an engine starts. It asks: self.engine.start().Garage has a list of cars. total_power goes through the cars and, for each, into its engine: car.engine.power. The dots read like a path: garage, car, engine, power.g.cars[0].drive(), only the first engine is running. The second car's engine is a different object, still off.Each class has one job, and the objects work together. An engine knows about power and starting. A car knows about driving. A garage knows about parking.
Engine, not mixed into Car.ElectricMotor with the same methods, and put it in the attribute. No family tree to rebuild.Ask the question in words: a circle is a shape (inheritance), and a circle has a centre (composition).
| Question | Answer: inheritance | Answer: composition |
|---|---|---|
| Is the sentence "A is a B" true? | yes | no |
| Does A use or contain a B? | no | yes |
| Will A need to swap the B? | no | yes |
| Do A and B share a lot of code and the same methods? | yes | not necessarily |
A famous rule of thumb from experienced programmers: prefer composition, and use inheritance only when the "is a" is clear. Many programs need no inheritance at all.
When you plan a program, draw the objects as boxes and the relations as arrows, and write on each arrow either is a or has a. For a school:
Student is a Person (inheritance).Teacher is a Person (inheritance).Classroom has Student objects (composition).Student has a list of Mark objects (composition).If an arrow will not take either label, the design needs another thought.
Stack that inherits from list is a list in disguise, and allows operations that a stack should not.garage.cars[0].engine.parts[2].serial. Give each class a method for what the outside needs.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:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Car an Engine, or does it have one?garage.cars[0].engine.power, what does each dot do?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.