Python
Class Inheritance
Class inheritance and method overriding.
By EZ4Code Team
classinheritanceoop
Code
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError
class Dog(Animal):
def speak(self):
return f"{self.name}: Woof"
class Cat(Animal):
def speak(self):
return f"{self.name}: Meow"
def make_sound(animal):
print(animal.speak())
make_sound(Dog("Buddy"))
make_sound(Cat("Kitty"))Explanation
Subclasses inherit from parent classes and override methods, demonstrating polymorphism.
More Python Snippets
Sort Dictionary by Value
Sort a Python dictionary by its values in descending order.
List Comprehension
Quickly generate lists using list comprehensions.
Dictionary Merging
Multiple ways to merge dictionaries.
File Read/Write
Various ways to read and write files.
CSV Processing
Read and write CSV files using the csv module.
JSON Processing
JSON serialization and deserialization.