Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is polymorphism in simple words in c#?

Explain practical use of polymorphism in c#

user-image
Question added by Atif Gulzar , Web Developer , Ethos Integrated Solutions
Date Posted: 2013/09/19
Ahmad Anbari
by Ahmad Anbari , Software System Engineer , Continental Jet Services FZCO

It is simply: same interface multiple implementations. There are two different types of polymorphism: static and dynamic.

 

The static one is decided at the design time, and the dynamic one is decided at the run time.

 

To make it clear let’s take this example: We write a Shape class which has a print method. As we all know a rectangle, square, and a circle are all shapes. So, they all inherit from the Shape class. Each one of them implements the print function to reflect itself to the printing device. The Rectangle draw a rectangle, the circle draws a circle and so forth. Let say way are writing a painting application that keep all the objects on the drawing sheet in a list of shapes so we can print it later. At the time the user click the print button in the painting application, we will ask the list of shapes to print itself. The list will ask every object to print himself. Every shape will print the corresponding shape that reflects himself. This way the list only calls the print method, regardless this shape is a circle, a rectangle or a square. 

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....

Osama Gamal
by 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
by 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. :)

Bowsil Ameen
by 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

Mohannad Bakbouk
by 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.

Jainul Abdeen Khan
by 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
by 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

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

Azmat Jadoon
by Azmat Jadoon , Software Engineer , NADRA

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

Hamid Anjum
by 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.

 

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.