xxxxxxxxxx
# Superclass
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
# Subclass inheriting from Animal
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def make_sound(self):
return "Woof!"
# Creating instances of the subclasses
animal = Animal("Generic Animal")
dog = Dog("Buddy", "Labrador")
# Accessing attributes and methods from the superclass
print(animal.name) # Output: "Generic Animal"
print(dog.name) # Output: "Buddy"
# Calling the method from the superclass
print(dog.make_sound()) # Output: "Woof!"
xxxxxxxxxx
# It's kinda hard to explain this just by code.
# So I'll provide a link to a pretty good explanation of it.
https://www.pythonforbeginners.com/super/working-python-super-function
xxxxxxxxxx
class Square(Rectangle):
def __init__(self, length):
super().__init__(length, length)
xxxxxxxxxx
class SuperClass:
def __init__(self):
pass
def example_method(self):
print('Hello, World!')
class SubClass(SuperClass):
def __init__(self):
pass
c = SubClass()
c.example_method()