Polymorphism

Learn how polymorphism lets the same interface behave differently in BornomalaScript.

Polymorphism

Polymorphism means the same interface can behave in different ways depending on the object using it.

That is useful when several objects share the same method name but do not do exactly the same thing.

Why Polymorphism Matters

Polymorphism helps you write flexible code.

Instead of writing separate logic for every object, you can call the same method and let the object decide how to respond.

Simple Example

Imagine different devices that all have a start action.

  • A phone starts one way
  • A computer starts another way
  • A game console starts another way

The command is the same, but the behavior differs.

Conceptual Example

dhoro Animal = {
	sound: kaj() {
		lekho("Some sound")
	}
}

dhoro Dog = Animal
Dog.sound = kaj() {
	lekho("Bark")
}

dhoro Cat = Animal
Cat.sound = kaj() {
	lekho("Meow")
}

Here, the same method name gives different output depending on the object.

How It Works

Polymorphism usually means:

  • one shared method name
  • multiple object types
  • different behavior for each type

This makes your code easier to extend later.

Step-by-Step Breakdown

  1. Define a common interface or method
  2. Create multiple objects that use it
  3. Change the behavior in each object
  4. Call the same method and let the object respond differently

Common Mistakes

Beginners often make these mistakes:

  • confusing polymorphism with inheritance
  • expecting every object to behave exactly the same
  • overcomplicating the example before understanding the shared interface

Practice Task

Try designing polymorphism for:

  1. Animals making sounds
  2. Shapes calculating area
  3. File types opening differently

Quick Checklist

Before moving on, make sure you can:

  • explain polymorphism in simple words
  • describe how the same method can behave differently
  • identify at least one real example of the idea

If yes, you understand the core OOP pattern of polymorphism in BornomalaScript.