You are currently viewing Inheritance and Polymorphism

Inheritance and Polymorphism

Inheritance and polymorphism are important concepts in object-oriented programming (OOP) that allow developers to create more efficient and reusable code.

Inheritance is the process by which a new class is created from an existing class. The new class, known as the derived class, inherits all the properties and methods of the existing class, known as the base class. The derived class can then add its own properties and methods or override the properties and methods of the base class.

Polymorphism refers to the ability of objects to take on multiple forms or to respond differently to the same message or method call. This allows for more flexibility in programming and can make code more reusable.

Here’s an example of inheritance and polymorphism in Python:

# Base class
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        pass

# Derived class 1
class Dog(Animal):
    def speak(self):
        return "Woof!"

# Derived class 2
class Cat(Animal):
    def speak(self):
        return "Meow!"

# Polymorphism example
def animal_speak(animal):
    print(animal.speak())

# Create instances of the classes
dog = Dog("Rex")
cat = Cat("Fluffy")

# Call the polymorphic function
animal_speak(dog)   # Output: Woof!
animal_speak(cat)   # Output: Meow!

In this example, we have a base class called Animal that has a constructor method that initializes the name property, and a speak() the method that is overridden in the derived classes. We then have two derived classes, Dog and Cat, which that inherits from the Animal class and override the speak() method to return the appropriate sound for each animal.

We also have a polymorphic function called animal_speak() that takes an object of any class that inherits from the Animal class and calls its speak() method. When we create instances of the Dog and Cat classes and pass them to animal_speak(), it automatically calls the appropriate speak() method for each animal. This demonstrates the flexibility and reusability of OOP concepts like inheritance and polymorphism.

Example

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        raise NotImplementedError("Subclass must implement abstract method")

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

class Cow(Animal):
    def speak(self):
        return "Moo!"

def animal_speak(animal):
    print(animal.name + " says " + animal.speak())

my_dog = Dog("Buddy")
my_cat = Cat("Whiskers")
my_cow = Cow("Bessie")

animal_speak(my_dog)  # Output: Buddy says Woof!
animal_speak(my_cat)  # Output: Whiskers says Meow!
animal_speak(my_cow)  # Output: Bessie says Moo!

In this example, we have an Animal class with an abstract speak method, which we define as raising a NotImplementedError because we want to enforce that any subclass of Animal must implement its own speak method.

We then define three subclasses of Animal: Dog, Cat, and Cow, each with its own implementation of the speak method.

Finally, we define a function called animal_speak that takes an Animal object as an argument and calls its speak method, printing out the result.

We create instances of Dog, Cat, and Cow, and pass them to animal_speak, demonstrating polymorphism in action: despite being different types of animals with different implementations of speak, they can all be passed to the same function and treated as instances of the base Animal class.

Leave a Reply