How Do You Establish Polymorphism in C# (C Sharp)

Polymorphism is an important feature of Object Oriented Programming. Polymorphism gives the ability to take multiple forms. In programming sense, when the same method takes more than one form, then it is polymorphism. This article will illustrate polymorphism with examples.




There are two different types of polymorphism namely:

• Static/Compile Time Polymorphism
• Dynamic/ Runtime Polymorphism

Static/Compile Time Polymorphism:

Static polymorphism is achieved using method overloading and operator overloading. Consider the following example:

class sampleClass {
int member1;
string member2;
public void setData(int member1) {
this.member1 = member1;
}
public void setData(string member2) {
this.member2 = member2;
}
public void setData(int member1, string member2) {
this.member1 = member1;
this.member2 = member2;
}
public void displayData() {
Console.WriteLine(“Member1:”+member1+” Member2:”+member2);
}
}
class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj.setData(10, “Hello!”);
Console.WriteLine(“Displaying Values of obj..”);
obj.displayData();
sampleClass obj1 = new sampleClass();
obj1.setData(“Good Day!”);
obj1.setData(100);
Console.WriteLine(“Displaying Values of obj1..”);
obj1.displayData();
}
}

Output of this code will be:

Displaying Values of obj..
Member1: 10 Member2: Hello!
Displaying Values of obj1..
Member1: 100 Member2: Good Day!

In this example, you defined the same method setData thrice with different set of arguments. This is known as method overloading. You would have done this frequently in constructors. You might have defined many constructors for the class with different set of arguments. In this example, you created testClass and instantiated sampleClass inside Main method of testClass. After instantiating, you have called different forms of setData based on your need. These forms of method calls are mapped to the actual methods during compile time.

Hence the name: compile time polymorphism. Ensure that the overloading methods must have the same name but the number of arguments or the type of arguments passed to each form of the method should be different.

Dynamic/ Runtime Polymorphism:

Dynamic polymorphism is implemented using method overriding. Consider the following example:

class baseClass {
virtual void displayMsg() {
Console.WriteLine(“In baseClass…”);
}
}
class derived1Class : baseClass {
override void displayMsg() {
Console.WriteLine(“In derived1Class…”);
}
}
class derived2Class : baseClass {
override void displayMsg() {
Console.WriteLine(“In derived2Class…”);
}
}
class testClass {
public static void Main() {
baseClass obj;
obj = new baseClass();
obj.displayMsg();
obj = new derived1Class();
obj.displayMsg();
obj = new derived2Class();
obj.displayMsg();
}
}

Output of this code will be:

In baseClass…
In derived1Class…
In derived2Class…

In this example, baseClass contains a virtual method called displayMsg. baseClass is then inherited by two different derived classes each of them overriding displayMsg method. Inside Main method of testClass, you create an instance of type baseClass using the following statement:

baseClass obj;

This object obj can now reference baseClass or derived1Class or derived2Class based on the actual instantiation done using new operator. For example,

baseClass obj = new derived1Class();

This statement will make the object obj reference the derived1Class.

Hence calling obj.displayMsg will execute displayMsg method in derived1Class even though the object obj is of type baseClass.

This binding of method call to the actual method happens at run time based on the reference made by the object. This is termed as dynamic/runtime polymorphism. Unlike method overloading, method overridden in the derived class should have the same signature as that of the base class method with the same number of arguments and same type of arguments.

SetApp - 100 Apps for everything you ever wanted to do on Mac

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How Do You Establish Polymorphism in C# (C Sharp) | How Do You Overload == Operator in C# (C Sharp) ? | How Do You Overload == Operator in C# (C Sharp) ? | How to Perform User Defined Conversions Between Structures (Struct) | Illustration of C# (C Sharp) Keywords (params, ref, out) Used to Declare | Introduction to Static Classes of C# (C Sharp) | Introduction to Static Members of C# ( C Sharp) | Overview of Structs in C# (C Sharp) | Using Constructors and Destructors in C# (C Sharp) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2019 - All Rights Reserved.