Lesson 26 of 55 · Python
OOP – Inheritance
Duration: 20 minutes
Lesson 26 of 55 · Python
Duration: 20 minutes
python\nclass Animal:\n def __init__(self, name):\n self.name = name\n def speak(self):\n raise NotImplementedError\n\nclass Dog(Animal):\n def speak(self):\n return 'Woof!'\n\nclass Cat(Animal):\n def speak(self):\n return 'Meow.'\n\npet = Dog('Rex')\nprint(pet.speak())\n\n\nThe Dog and Cat classes inherit __init__ from Animal and provide their own speak implementation.\n