About Lesson
Superclass and Subclass:
- Definition: Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass). python
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!"
- Explanation: The
Dog
class inherits from theAnimal
class. Thespeak
method is overridden in theDog
class.
- Explanation: The
Method Overriding:
- Definition: Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. python
class Cat(Animal): def speak(self): return f"{self.name} says Meow!"
- Explanation: The
Cat
class overrides thespeak
method of theAnimal
class to provide its own implementation.
- Explanation: The