conceptoopsystemsPolymorphism (“Many Forms”) allows objects of different classes to be treated as objects of a common superclass. It is the ability to call the same method draw() on a list of objects, and have the Circle draw a circle and the Square draw a square.
1. Dynamic Polymorphism (Runtime)
This uses Method Overriding. The specific method to call is determined at runtime.
C++ (Virtual Functions)
Must use virtual keyword and pointers.
class Shape {public: virtual void draw() { cout << "Generic Shape"; }};class Circle : public Shape {public: void draw() override { cout << "Drawing Circle"; }};// UsageShape* s = new Circle();s->draw(); // Calls Circle::draw()
Java (Interfaces)
All non-static methods are virtual by default.
interface Shape { void draw(); }class Circle implements Shape { public void draw() { System.out.println("Circle"); }}
Rust (Traits)
Uses Trait Objects (Box<dyn Trait>) for dynamic dispatch.