This module covers inheritance in Python, focusing on building class hierarchies, method overriding, multiple inheritance, and advanced class design principles.
- Solution
- Description:
Task "Edible or Inedible":
- Implement an animal kingdom hierarchy using inheritance. Create parent classes
AnimalandPlant, and child classesMammal,Predator,Flower, andFruit. Include methods for animals to eat plants, where animals get fed if the plant is edible and die if it is not.
- Implement an animal kingdom hierarchy using inheritance. Create parent classes
- Solution
- Description:
Task "Cannot Change, Only Get":
- Implement a
Vehicleclass and its subclassSedan. TheVehicleclass should have private attributes for model, engine power, and color, which can be retrieved but not directly modified. TheSedanclass should inherit these properties and include an additional passenger limit.
- Implement a
- Solution
- Description:
Task "Mythical Inheritance":
- Create classes
Horse,Eagle, andPegasus.Pegasusshould inherit from bothHorseandEagle, and include methods for both running and flying, as well as returning its current position and sound.
- Create classes
- Solution
- Description:
Task "They All Look So Alike":
- Implement a hierarchy of 2D and 3D geometric shapes using classes. Create a base class
Figure, and subclassesCircle,Triangle, andCube. Each shape should have methods for managing dimensions, colors, and calculating area or volume.
- Implement a hierarchy of 2D and 3D geometric shapes using classes. Create a base class