Polymorphism دو لفظوں سے مل کر بنا ہے جس کا مطلب ہے کہ کئی شکلیں یعنی کثیر الاشکال۔ اس کی مدد سے ذیلی کلاس(subclass) کے قاعدوں(methods) کے نام وہی ہوتے ہیں جو سپر کلاس کے قاعدوں کے ہوتے ہیں۔ اس سے پروگرام میں یہ قابلیت پیدا ہو جاتی ہے کہ وہ object کی ٹائپ کے مناسبت سے درست قاعدے کا اطلاق کر سکے۔
polymorphism کسی object کی کئی شکلوں میں ڈھل جانے کی خصوصیت کے متعلق بات کرتی ہے۔ یہ OOP کی ایک بہت طاقتور خصوصیت ہے جس سے پروگرامر عمومیت پر دھیان دے کر خاص اور منفرد حالات اور کیس کو پروگرام کی انجام دہی کے وقت کے لیے چھوڑ سکتے ہیں۔

PHP:
# A quick example of polymorphism at work in python
class Food(object):
    def __init__(self, name, calories):
        self.name = name
        self.calories = calories
    def tastesLike(self):
        raise NotImplementedException("Subclasses are responsible for creating this method")
 
class Biryani(Food):
    def tastesLike(self):
        return "Extremely tasty rice dish"
 
class Karahi(Food):
    def tastesLike(self):
        return "grilled goodness and deliciousness beyond words"
 
class SaagMakai(Food):
    def tastesLike(self):
        return "tastes like heaven"
 
dinner = []
dinner.append(Biryani('Chicken/Mutton Rice', 430))
dinner.append(Karahi('High fat Chicken/Mutton ', 460))
dinner.append(SaagMakai('Sabzi Rooti', 270))
 
# even though each course of the dinner is a differnet type
# we can process them all in the same loopn
for course in dinner:
    print(course.name + " is type " + str(type(course)))
    print("  has " + str(course.calories) + " calories")
    print("  tastes like " + course.tastesLike())

پروگرام کے نتائج مندرجہ ذیل ہوں گے۔

کوڈ:
Chicken/Mutton Rice is type <class '__main__.Biryani'>
  has 430 calories
  tastes like Extremely tasty rice dish
Highfat Chicken/Mutton  is type <class '__main__.Karahi'>
  has 460 calories
  tastes like grilled goodness and deliciousness beyond words
Saag Makai Rooti is type <class '__main__.SaagMakai'>
  has 270 calories
  tastes like tastes like heaven
 
Top