Polymorphism in C#: Introduction and Types

javaguitutorialThe word polymorphism can be broken down into two different words, ‘poly’ meaning many and ‘morph’ meaning forms, and hence the meaning ‘having many forms’. In C#, or for that matter in any object oriented programming language, polymorphism is used to imply one name with multiple functionality. In polymorphism we declare methods with the same name and different parameters in same class or methods with the same name and same parameters in different classes. Polymorphism has the ability to provide different implementation of methods that are implemented with the same name.

Before we begin our detailed discussion about polymorphism, it would be a good idea to learn some of the basic programming techniques in C#. Learning these basic programming techniques is important because the concept which is being discussed here, polymorphism, can be understood completely and effectively only when you will be familiar with the other related concepts like classes or methods. Hence it is important to learn the basics. If you are a complete beginner in C# then it is highly advisable for you to go through this programming course for complete beginners in C#. This course will help you to develop an understanding of the language and will enable you to understand the concepts which are being discussed here. You can also read this blog post about learning C# to further enhance your knowledge about this programming language.

Now that we have the basic understanding about programming in C#, let us begin our discussion about polymorphism.

We have already discussed the definition of polymorphism. Now we are going to discuss the two types of polymorphism in detail. These are static polymorphism and dynamic polymorphism.

Static Polymorphism

In static polymorphism, the response to a function is determined at the time of compiling and hence it is also known as compile time polymorphism. In this polymorphism, the methods are overloaded with the same names but are given different signatures. Hence it is also called method overloading. A method in C# is basically a group of statements that combine to perform a task. Here is an amazing course about the introduction of programming in C# which will teach you about methods and other related concepts in C# and will help you in understanding static polymorphism in a better way.

Let us try and understand the concept of static polymorphism with the help of an example.

public class addition
{
public int Add(int a, int b)
{
   return a+b;
}
public double Add(int x, int y, int z)
{
return x+y+z;
}
 }

In this example, we have a class ‘addition’ having two functions with the same name ‘Add’ but with two different input parameters. First function contains two parameters while the second function contains three parameters. Hence this polymorphism is also known as method overloading. In this polymorphism, the compiler knows about the type of object it is linking to and it is also aware about the methods which are going to be used to call it and hence the name compile time polymorphism.

Two techniques are provided in C# for the implementation of static polymorphism, function overloading and operator overloading. Function overloading means having multiple definitions for the same function name in the same scope. The definitions of the function must differ from each other on the basis of types or the number of arguments in the argument list. The function declarations which differ from each other only by the return type cannot be successfully overloaded.

In operator overloading, the type of the operator is defined by the user. Overloaded operators are the functions with special names, the keyword operator followed by the symbol for the operator being defined. The overloaded operators, like any other function, have a return type and a parameter list. The function overloading and the operator overloading are the two different techniques for the implementation of static polymorphism in C# and these have their own particular areas of application. They should only be used for the right program as per the requirements as the incorrect implementation will only lead to errors.

Dynamic Polymorphism

In dynamic polymorphism, the response to a function is determined at run time and hence is known as run time polymorphism. In this polymorphism, the different methods have the same name and also the same signature but differ in the implementation. In this polymorphism the methods are overridden and hence it is also known as method overriding. This course about the programming techniques in C# will help you in understanding these concepts in a better way.

Now let us try and understand the concept of polymorphism with the help of an example.

public class shape
{
public int _radius = 5;
public virtual double getArea()
{
return 0;
}
}
public class circle : shape
{
public override double getArea()
{
return 3.14 * _radius * _radius;
}
}
public class sphere : shape
{
public override double getArea()
{
return 4 * 3.14 * _radius * _radius;
}
}

In this example, we have used one base class ‘shape’ with a virtual method ‘getArea()’ and two derived classes ‘circle’ and ‘sphere’, each having an override method ‘getArea()’ with different implementations. In this example, the class ‘shape’ and a function ‘getArea’ have been declared. Then two other classes ‘circle’ and ‘sphere’ have been declared which give the output as the area of the circle and the area of the sphere, using the same function ‘getArea()’, as per the input which is provided. The ‘virtual’ and ‘override’ keywords are used for method overriding. This is how the dynamic polymorphism works.

This was all about the different types of polymorphism in C#. Here, we discussed the concept as well as its different types in detail. The different types of polymorphism were studied with the help of relevant examples and all the related concepts were also properly discussed. Till now, our discussion has been theory based. We have discussed the examples and the code for the different types of polymorphism but have not actually written or complied any code to see how it actually works. To become a master in any programming language, it is very important to supplement the theoretical learning with the right practical training to completely understand all the concepts.

We have studied all the concepts theoretically and so now the next logical step for us would be to implement these concepts to some practical problems to gain valuable experience and to iron out any flaws that might be there. This beginners course in C# which provides a number of useful lessons and exercises will provide you with all the resources you will require to refine your knowledge and to gain the all important practical knowledge about polymorphism. This course has been specifically designed keeping the requirements of the beginners in C# in mind and hence it will prove equally beneficial for the users of all experience levels. So take this course and start learning. Good luck!