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
Animal
andPlant
, 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
Vehicle
class and its subclassSedan
. TheVehicle
class should have private attributes for model, engine power, and color, which can be retrieved but not directly modified. TheSedan
class should inherit these properties and include an additional passenger limit.
- Implement a
- Solution
- Description:
Task "Mythical Inheritance":
- Create classes
Horse
,Eagle
, andPegasus
.Pegasus
should inherit from bothHorse
andEagle
, 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