In this lab, we’ll delve deeper into object-oriented programming by exploring polymorphism, a core concept that enhances flexibility and efficiency in our code. We'll use our existing Plant program to illustrate how polymorphism allows objects to interact dynamically through method overriding.
By the end of this lab, you will apply these concepts to further develop your "Animal Kingdom" program. This will not only reinforce your understanding but also demonstrate how these principles can be applied in various programming scenarios.
Ensure that you are signed into your GitHub account
Join the GitHub Classroom and accept this assignment: Project 10 - Animal Kingdom
Clone the repository to your computer using GitHub Desktop or open it online using Codespaces
Polymorphism is a fundamental concept in object-oriented programming that allows objects to be treated as instances of their parent class, even if they are actually instances of a subclass. This ability enables us to use a unified interface for different data types. Polymorphism comes in several forms, including:
Method Overriding: Where a subclass provides a specific implementation of a method that is already defined in its superclass.
Method Overloading: Where two or more methods in the same class have the same name but different parameters.
(Note: Python does not support method overloading by default, but similar behavior can be mimicked with some setting up.)
Flexibility: It allows for the writing of more flexible and easily maintainable code. You can call the same method on different objects and each object can respond in a way appropriate to itself.
Ease of Extension: Polymorphism allows programmers to extend programs without modifying the existing codebase. New object types can be introduced that still interact seamlessly with the older elements of the system.
Method overriding is a technique where the subclass provides a specific implementation for a method that was already defined in the superclass.
This is a powerful feature of polymorphism that allows for dynamic interactions in a system.
Consider enhancing our plant classes by overriding a common method called display_info() that behaves differently depending on the plant type:
class Plant:
def display_info(self):
return f"{self.name} is a plant in the {self.habitat} habitat."
class Flower(Plant):
def display_info(self):
return f"{self.name} is a {self.color} flower that blooms in the {self.habitat}."
class Tree(Plant):
def display_info(self):
return f"{self.name} is a tree that grows to {self.height} feet tall in the {self.habitat}."
class Cactus(Plant):
def display_info(self):
return f"{self.name} is a cactus with flowers: {self.has_flowers} in the {self.habitat}."
Let's see how we can use polymorphism through method overriding in a complete program scenario:
Open lab1.py
Complete the following activities:
Paste your base class Animal and the derived classes from Lab 1 here.
Modify the classes to demonstrate polymorphism through method overriding.
Each derived class should override at least one method from the Animal class.
For instance, you might want to override a 'make_sound' method to reflect the specific sound each animal makes.
Create at least two instances of your derived classes
Call the overridden methods on the instances.
Commit and push your code