Skip to main content

Polymorphism

What is Polymorphism?

Polymorphism is one of the core concepts of Object-Oriented Programming (OOP). The word comes from Greek and means "many forms."

In programming, polymorphism allows different classes to define methods with the same name, but different behavior. This means that you can call the same method name on different types of objects, and each object will respond in its own way.

Why is Polymorphism Useful?

Polymorphism helps you:

  • Write cleaner and more flexible code
  • Treat objects of different types in a uniform way
  • Add new classes with less code changes
  • Increase reusability and scalability of your programs

Types of Polymorphism

There are two main types of polymorphism:

  1. Compile-time polymorphism (also called method overloading – not currently supported in EasyBite)
  2. Run-time polymorphism (also called method overriding – supported in EasyBite)

EasyBite supports run-time polymorphism through method overriding.


Method Overriding in EasyBite

Definition

When a subclass defines a method that already exists in its parent class with the same name, it overrides the parent’s method. This allows the subclass to provide its own version of the method.


Example: Method Overriding

class Animal
method speak()
show "Animal makes a sound"
end method
end class

class Dog inherit Animal
method speak()
show "Dog says: Woof!"
end method
end class

class Cat inherit Animal
method speak()
show "Cat says: Meow!"
end method
end class

set a1 to new Dog()
set a2 to new Cat()

a1.speak()
a2.speak()

Output

Dog says: Woof!
Cat says: Meow!

Explanation

  • Dog and Cat both inherit from Animal.
  • They both override the speak method.
  • Even though we called the same method name (speak), each object responded differently.

Polymorphism with Loops (Dynamic Behavior)

You can also use polymorphism to work with a list of objects, treating them as the same type but calling the overridden methods dynamically.

Example

class Bird
method fly()
show "Bird flies"
end method
end class

class Eagle inherit Bird
method fly()
show "Eagle flies high"
end method
end class

class Penguin inherit Bird
method fly()
show "Penguin cannot fly"
end method
end class

set flock to [new Bird(), new Eagle(), new Penguin()]

foreach b in flock
b.fly()
end foreach

Output

Bird flies
Eagle flies high
Penguin cannot fly

Explanation

  • All the objects are instances of classes that share the same method fly.
  • When we loop over the list and call b.fly(), the correct version of the method is chosen based on the object’s actual class.
  • This is runtime polymorphism in action.

Real-World Analogy

Imagine you have a remote control (the method name) and multiple devices (objects) like a TV, fan, and AC. When you press the power button (same method), each device responds in its own way—TV turns on, fan starts rotating, AC begins cooling.

The same command (power) behaves differently depending on the object it is acting on. That’s polymorphism!


Summary

  • Polymorphism allows methods with the same name to behave differently depending on the object.
  • It promotes flexibility, extensibility, and reusability in your code.
  • In EasyBite, polymorphism is achieved through method overriding using inherit.