Question 54: Write a Python program to implement multiple inheritance. Download hole Program / Project code, by clicking following link: Write a program in Python to demonstrate following operations: Simple inheritance, Multiple inheritance ? Certainly! Here's a Python program that demonstrates both simple inheritance and multiple inheritance: # Simple Inheritance
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
# Multiple Inheritance
class CanFly:
def fly(self):
print("I can fly")
class CanSwim:
def swim(self):
print("I can swim")
class Bird(CanFly, Animal):
def speak(self):
print("Bird chirps")
class Fish(CanSwim, Animal):
def speak(self):
print("Fish bubbles")
# Main program
if __name__ == "__main__":
# Simple Inheritance
animal = Animal()
dog = Dog()
print("Simple Inheritance:")
animal.speak() # Calls Animal's speak method
dog.speak() # Calls Dog's speak method (overrides Animal's speak)
# Multiple Inheritance
bird = Bird()
fish = Fish()
print("\nMultiple Inheritance:")
bird.speak() # Calls Bird's speak method (overrides Animal's speak)
bird.fly() # Calls CanFly's fly method
fish.speak() # Calls Fish's speak method (overrides Animal's speak)
fish.swim() # Calls CanSwim's swim method
Output:
Simple Inheritance:
Animal speaks
Dog barks
Multiple Inheritance:
Bird chirps
I can fly
Fish bubbles
I can swim
In this program:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
# Multiple Inheritance
class CanFly:
def fly(self):
print("I can fly")
class CanSwim:
def swim(self):
print("I can swim")
class Bird(CanFly, Animal):
def speak(self):
print("Bird chirps")
class Fish(CanSwim, Animal):
def speak(self):
print("Fish bubbles")
# Main program
if __name__ == "__main__":
# Simple Inheritance
animal = Animal()
dog = Dog()
print("Simple Inheritance:")
animal.speak() # Calls Animal's speak method
dog.speak() # Calls Dog's speak method (overrides Animal's speak)
# Multiple Inheritance
bird = Bird()
fish = Fish()
print("\nMultiple Inheritance:")
bird.speak() # Calls Bird's speak method (overrides Animal's speak)
bird.fly() # Calls CanFly's fly method
fish.speak() # Calls Fish's speak method (overrides Animal's speak)
fish.swim() # Calls CanSwim's swim method
Output:
Simple Inheritance:
Animal speaks
Dog barks
Multiple Inheritance:
Bird chirps
I can fly
Fish bubbles
I can swim
In this program:
- Simple Inheritance: We have a base class Animal with a speak method, and a subclass Dog that inherits from Animal. The Dog class overrides the speak method.
- Multiple Inheritance: We have two classes, CanFly and CanSwim, each providing a specific capability. Then, we have two classes, Bird and Fish, that inherit from both Animal and one of the capability classes. The Bird class inherits the ability to fly from CanFly, and the Fish class inherits the ability to swim from CanSwim.
As you can see, simple inheritance allows the Dog class to inherit and override the speak method from the base class Animal. In multiple inheritance, the Bird and Fish classes inherit properties and behaviors from both Animal and specific capability classes (CanFly and CanSwim).
Programming Code:
Following code write in: BP_P54.py
# Python Program # Multiple Inheritance class Car1(): # Attributes name = "Jaguar" vehicle_type = "Four-wheeler" price = 9876034 # Method def show1(self): print("Car Name: ", self.name) print("Type: ", self.vehicle_type) print("Price: ", self.price) class Car2(): # Attribute name = "BMW 3" vehicle_type = "Four-wheeler" print = 4569807 # Method def show2(self): print("Car Name: ", self.name) print("Type: ", self.vehicle_type) print("Price: ", self.print) # Inheritance class Vehicle(Car1, Car2): print("******************* Vehicles *******************") # Create object obj = Vehicle() # Call Method obj.show1() obj.show2() # Thanks for Reading.
Output:
0 Comments