Python Practice › OOP Programs › Program 48

Dog inherits from Animal

EasyOOP Programs · 48 of 48

Create Dog (inherits Animal) with breed and weight, and a method to print all details.

Advertisement
Output
Bruno is brown, a Labrador weighing 28 kg

Approach

  1. Override show and reuse the parent’s message with super().

Solution

Python
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.

More oop programs

← All 48 OOP Programs

Leave a Comment