class LivingBeing: def init(self, species): self.species = species
def make_sound(self):
pass
class Canine(LivingBeing): def make_sound(self): return "Bark!"
Usage
wolf = Canine("Gray Wolf") print(wolf.make_sound()) # Output: Bark! print(wolf.species) # Output: Gray Wolf
Notice how Python automatically calls the parent constructor if not explicitly defined. For multiple inheritance: ```
class Engineer:
def __init__(self):
self.skill = "Coding"
def develop(self):
return "Building software"
class Artist:
def __init__(self):
self.talent = "Painting"
def create(self):
return "Making art"
class CreativeTechnologist(Engineer, Artist):
def __init__(self):
Engineer.__init__(self)
Artist.__init__(self)
self.role = "Hybrid creator"
def showcase(self):
return f"{self.skill} meets {self.talent}"
# Usage
creator = CreativeTechnologist()
print(creator.showcase())
Dynamic Composition ===== Python allows runtime addition of attributes and methods: ```
class DocumentWriter: def write(self, text): return f"Writing: {text}"
class DocumentReader: def read(self): return "Reading content"
class SmartDevice: pass
Dynamic composition
device = SmartDevice() device.writer = DocumentWriter() device.reader = DocumentReader()
Usage
print(device.writer.write("Hello")) # Output: Writing: Hello print(device.reader.read()) # Output: Reading content
Polymorphism Example ===== Python implements polymorphism similarly to Java: ```
class Vehicle:
def move(self):
raise NotImplementedError("Implement in subclass")
class Car(Vehicle):
def move(self):
return "Driving on roads"
class Boat(Vehicle):
def move(self):
return "Sailing on water"
def transport(vehicle):
print(vehicle.move())
# Usage
my_car = Car()
my_boat = Boat()
transport(my_car) # Output: Driving on roads
transport(my_boat) # Output: Sailing on water