Python Practice › OOP Programs › Program 29

Cat inherits from Animal

EasyOOP Programs · 29 of 48

Create a Cat class that inherits from Animal (name, colour) and adds breed and weight.

Advertisement
Output
Milo | grey | Persian | 4.2 kg

Approach

  1. Extend the parent’s __init__ and add a show method.

Solution

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

More oop programs

← All 48 OOP Programs

Leave a Comment