Inheritance and Polymorphism


Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP) that are also supported in Python. They allow you to create reusable and extensible code by establishing relationships between classes.

Inheritance

Inheritance is a mechanism that allows a class (known as the child or derived class) to inherit the properties and methods of another class (known as the parent or base class). The child class can then add additional attributes or behaviors, or override the existing ones.

Here's an example:


In this example, the Dog class inherits from the Animal class. This means that the Dog class has all of the attributes and methods of the Animal class, plus any additional attributes and methods that are defined in the Dog class.

In the example above, the Dog class has a method called bark(). This method is not defined in the Animal class, so it is a new method that is specific to the Dog class. When we create an object of the Dog class, we can call the bark() method on that object. We can also call the speak() method on that object, even though the speak() method is defined in the Animal class.

This is because the Dog class inherits from the Animal class. Inheritance is a powerful feature of object-oriented programming that can help us to write more concise and reusable code.

Here are some other examples of inheritance in Python:

A Car class can inherit from a Vehicle class. A Dog class can inherit from an Animal class. A Rectangle class can inherit from a Shape class.

Inheritance can be used to create a hierarchy of classes, where each class inherits from a more general class. This can make it easier to understand and manage large codebases.



Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables you to write code that can work with objects of multiple types, providing flexibility and reusability.

Here's an example:


This code will print the following output: Woof! Meow! Even though the make_all_animals_sound() function takes a list of Animal objects, it can still call the make_sound() method on objects of different types, such as Dog and Cat.

This is because the make_sound() method is defined differently in each of these classes.

Polymorphism makes code more flexible and reusable. It allows us to write code that can work with different types of objects without having to change the code itself.

Here are some other examples of polymorphism in Python:

The + operator can be used to add integers, strings, and lists. The len() function can be used to get the length of a string, a list, or a tuple. The sorted() function can be used to sort a list of integers, a list of strings, or a list of objects.

In summary, inheritance allows you to create hierarchies of classes, while polymorphism enables you to treat objects of different classes as if they belong to a common type, leading to more flexible and reusable code.

Enroll Now

  • Python Programming
  • Machine Learning