Python Practice › OOP Programs › Program 29
Cat inherits from Animal
Create a Cat class that inherits from Animal (name, colour) and adds breed and weight.
Output
Milo | grey | Persian | 4.2 kg
Approach
- Extend the parent’s
__init__and add a show method.
Solution
class Animal: def __init__(self, name, color): self.name, self.color = name, color class Cat(Animal): def __init__(self, name, color, breed, weight): super().__init__(name, color) self.breed, self.weight = breed, weight def show(self): print(f"{self.name} | {self.color} | {self.breed} | {self.weight} kg") Cat("Milo", "grey", "Persian", 4.2).show()Output
Milo | grey | Persian | 4.2 kg
Learn the concept
New to this topic? Read the lesson, then come back.