Inheritance_Examples_Python
Inheritance_Examples_Python
Single Inheritance
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Animal makes a sound"
Multiple Inheritance
# Parent class 1
class Bird:
def fly(self):
return "I can fly!"
# Parent class 2
class Fish:
def swim(self):
return "I can swim!"
Multilevel Inheritance
# Base class
class Vehicle:
def info(self):
return "This is a vehicle"
Hierarchical Inheritance
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some generic animal sound"
# Child class 1
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
# Child class 2
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Child class 3
class Cow(Animal):
def speak(self):
return f"{self.name} says Moo!"