Python Practice › OOP Programs › Program 48
Dog inherits from Animal
Create Dog (inherits Animal) with breed and weight, and a method to print all details.
Output
Bruno is brown, a Labrador weighing 28 kg
Approach
- Override
showand reuse the parent’s message withsuper().
Solution
class Animal: def __init__(self, name, color): self.name, self.color = name, color def show(self): return f"{self.name} is {self.color}" class Dog(Animal): def __init__(self, name, color, breed, weight): super().__init__(name, color) self.breed, self.weight = breed, weight def show(self): return super().show() + f", a {self.breed} weighing {self.weight} kg" print(Dog("Bruno", "brown", "Labrador", 28).show())Output
Bruno is brown, a Labrador weighing 28 kg
Learn the concept
New to this topic? Read the lesson, then come back.