How are classes created in Python?

By ayush goel in 22 Sep 2023 | 04:10 pm
ayush goel

ayush goel

Student
Posts: 346
Member since: 21 Sep 2023

How are classes created in Python?

22 Sep 2023 | 04:10 pm
0 Likes
divas goyal

divas goyal

Student
Posts: 453
Member since: 22 Sep 2023

class Animal:

    def __init__(self, name):

        self.name = name


    def speak(self):

        pass


class Dog(Animal):

    def speak(self):

        return f"{self.name} says Woof!"


class Cat(Animal):

    def speak(self):

        return f"{self.name} says Meow!"


dog = Dog("Buddy")

cat = Cat("Whiskers")


print(dog.speak())  # Output: Buddy says Woof!

print(cat.speak())  # Output: Whiskers says Meow!


22 Sep 2023 | 06:28 pm
0 Likes

Report

Please describe about the report short and clearly.