Introduction
Polymorphism means having many forms in programming. The ability of the function to process the message or data in more than one form is called function overloading. When inheritance is used to extend a generalized class to a more specialized class, it includes the behavior of the top class (generalized). It is important that a given instance of an object uses the correct behavior and the property of polymorphism allows this to happen automatically. I have written this article focusing on students and beginners.
Flow Chart
Simple Keyword
Polymorphism is a message to be displayed in more than one form (or) the ability of an object to respond differently to different messages.
Method Overloading
Method overloading means Early binding or Static polymorphism. Static polymorphism is decided at compile-time only. Method overloading is a same method or function name, same return type but different input parameters.
Step 1
- Create a class Name “Common” (or) keep as it as your wish.
- To create a same method name and parameters.
- Write the following code to create a method overloading.
Coding
- public class Common
- {
- public int Add(int x,int y)
- {
- return (x + y);
- }
- public int Add(int x,int y,int z)
- {
- return (x + y + z);
- }
-
- }
Step 2
- Create a class Name “Test” (or) keep as it as your wish.
- To create a method, call the class named “Common” and implement the method.
- Write the following code
Coding
- public class Test
- {
- public static void Main(string[] args)
- {
- Common Ocommon = new Common();
- int Total= Ocommon.Add(10, 20, 30);
- Console.WriteLine("Total =" + Total);
- }
- }
Method overriding
Method overriding means late binding or dynamic polymorphism. Dynamic polymorphism is decided at Run-time only. Method overriding is a same method or function name, same input parameters, same return type but different class name.
Coding
- public class Program
- {
- public int Add(int x,int y)
- {
- return (x + y);
- }
-
- }
- public class MyClass
- {
- public int Add(int x,int y)
- {
- return (x + y);
- }
-
- }
- public class TestClass
- {
- static void Main(string[] args)
- {
- Program Oprogram = new Program();
- int Total= Oprogram.Add(10, 20);
- Console.WriteLine("Total =" + Total);
- MyClass Omyclass = new MyClass();
- int Subtotal = Omyclass.Add(10, 25);
- Console.WriteLine("Total =" + Subtotal);
- }
- }
Difference between Method overloading and Method overriding
Method overloading
|
Method overriding
|
Same method name, same return type but different input parameters
|
Same method name, same input parameters, same return type but different class name
|
Method overloading doesn’t need inheritance
|
Method overriding need inheritance
|
Any Access modifier
|
Public Access modifier only
|
Method overloading is early binding
|
Method overriding is late binding
|
Method overloading is decided at compile-time only
|
Method Overriding is decided at Run-time only
|
Summary
In this article, you have got an overview of Polymorphism in C#.I have written this article focusing on beginners and students.