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 show and reuse the parent’s message with super().
Solution
Python
classAnimal:def__init__(self,name,color):self.name,self.color=name,colordefshow(self):returnf"{self.name} is {self.color}"classDog(Animal):def__init__(self,name,color,breed,weight):super().__init__(name,color)self.breed,self.weight=breed,weightdefshow(self):returnsuper().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.
Create Animal with name and colour and a method to print them.
Output
Bruno is brown
Approach
Store and print.
Solution
Python
classAnimal:def__init__(self,name,color):self.name,self.color=name,colordefshow(self):print(f"{self.name} is {self.color}")Animal("Bruno","brown").show()
Output
Bruno is brown
Learn the concept
New to this topic? Read the lesson, then come back.