Polymorphism
Source: 8-Class And Objects/8.3-Polymorphism.ipynb
Start here — no coding background needed
What you will learn
Same action, different behavior per type.
In simple words
Different classes can share method names like `speak()` but do different things.
Group data and actions together — used in bigger apps; go slow, use analogies.
Easy example — run this first. Change values and press Run again.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Reference notes (from full bootcamp)
Optional — deeper detail for when you are ready
Polymorphism
Polymorphism is a core concept in Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It provides a way to perform a single action in different forms. Polymorphism is typically achieved through method overriding and interfaces
Method Overriding
Method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class.
Expected (from notebook): Woof! Meow! Woof!
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): the area is 20 the area is 28.259999999999998
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Polymorphism with Abstract Base Classes
Abstract Base Classes (ABCs) are used to define common methods for a group of related objects. They can enforce that derived classes implement particular methods, promoting consistency across different implementations.
Expected (from notebook): Car enginer started
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Conclusion
Polymorphism is a powerful feature of OOP that allows for flexibility and integration in code design. It enables a single function to handle objects of different classes, each with its own implementation of a method. By understanding and applying polymorphism, you can create more extensible and maintainable object-oriented programs.
Practice test — try yourself
Write code, press Check. Wrong answer shows the correct code to copy & run.
You learned "Polymorphism". Use print() to show: Done: Polymorphism
Hint: Use one print() with the exact text.