ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is polymorphism in simple words in c#?

Explain practical use of polymorphism in c#

user-image
تم إضافة السؤال من قبل Atif Gulzar , Web Developer , Ethos Integrated Solutions
تاريخ النشر: 2013/09/19
Osama Gamal
من قبل Osama Gamal , Software Consultant , Ministry of Interior

in very breif words (many, much)

Polymorphism means having more than one form. Overloading and overriding are used to implement

Zubair Ali
من قبل Zubair Ali , Software Developer III , S&P Global

WROX explains it well by giving an example of .Net framework by saying "Every type in .Net is Ploymorphic as we can cast it to .toString()"This example may make you understand polymorphysim well that Polymorphic is basically a behaviour (in general english). In CS it means when a type behaves differently in various scenarios. I.e. 

class Person {}   class PersonDetails : Person {}  class Employee : PersonDetails {}

 

Now here you use to create an object of EMPLOYEE everytime whereas you cast it to PersonDetails or Person (as per your need) and then change its behaviour. :)

Mukhtar Ahmad
من قبل Mukhtar Ahmad

Polymorphism refers to having more than one forms i.e. having many functionalities..

for example: take example of TV, when you connect it to SET top box or Cable it woks as TV, when you connect it to computer interface it works as Monitor, and on connecting it to video game interface TV works as Game monitor and so on...

similiarly polymorphism can be understood...

hope you are getting my explanation....

Jainul Abdeen Khan
من قبل Jainul Abdeen Khan , Admin & IT Manager , Pirelli Qatar

Polymorphism refered to as one name many forms or having one name with multiple functionality.

In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.

using System;

namespace ProgramCall

{

    class Class1

    {

        public int Sum(int A, int B)

        {

            return A + B;

        }

        public float Sum(int A, float B)

        {

            return A + B;

        }

    }

    class Class2 : Class1

    {

        public int Sum(int A, int B, int C)

        {

            return A + B + C;

        }

    }

    class MainClass

    {

        static void Main()

        {

            Class2 obj = new Class2();

            Console.WriteLine(obj.Sum(10,20));

            Console.WriteLine(obj.Sum(10,15.70f));

            Console.WriteLine(obj.Sum(10,20,30));

            Console.Read();

        }

    }

}

Mahmoud Manaa
من قبل Mahmoud Manaa , Network Engineer , Zain - Kuwait

In a simple words :

Inheritance :  means that if you create a class Car with a public field TankSize then you derive from it a class SuperCar the last one has inherited the field TankSize from Car.

Polymorphism :  is the fact that every time in the code you have a method where a Car is expected you can pass a SuperCar and it will behave like a Car. with virtual methods defined as needed you will be calling a method on a base class but the actual object on which you are working on will execute its version of the virtual method so you will be calling SuperCar.GetPrice and not Car.GetPrice in fact.

(Davide Piras - StackOverFlow.com)

For Further Information: stackoverflow.com  and Microsoft Developer Network

Hope This Help ... Regards

Azmat Jadoon
من قبل Azmat Jadoon , Software Engineer , NADRA

In short, polymorphism is the change in behavior of an object at runtime.

Mohannad Bakbouk
من قبل Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb

Polymorphism is one of the fundamental concepts of OOP.

 

Polymorphism provides following features: 

It allows you to invoke methods of derived class through base class reference during runtime.

It has the ability for classes to provide different implementations of methods that are called through the same name.

Polymorphism is of two types: 

Compile time polymorphism/Overloading

Runtime polymorphism/Overriding

Compile Time Polymorphism

 

Compile time polymorphism is method and operators overloading. It is also called early binding.

 

In method overloading method performs the different task at the different input parameters.

 

Runtime Time Polymorphism

 

Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

 

When overriding a method, you change the behavior of the method for the derived class.  Overloading a method simply involves having another method with the same prototype.

 

Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.

Bowsil Ameen
من قبل Bowsil Ameen , Sharepoint Development officer / architect , Etihad Airways

Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms hence the name polymorphism. Polymorphism also refered to as one name many forms or having one name with multiple functionality.In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.

Two  types of PolymorphismStatic PolymorphismDynamic Polymorphism

Adnan Hamdani
من قبل Adnan Hamdani

Polymorphism means one name many Form, where you can write two function with same name and different signature and difference return type.

Hamid Anjum
من قبل Hamid Anjum , Team Lead \ Sr. Software Engineer , University of Business and Technology, Jeddah

In simple words polymorphism means1 thing has multiple forms :)when you write methods with the same name &  same number of parameters but  different data types & implement different behaviour in these methods. Now that is also polymorphism.

Other ways to implement polymorphism is operator overloading and implementing interfaces.

 

المزيد من الأسئلة المماثلة