Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.
Polymorphism : The word 'Polymorphism ' means 'many form' so Polymorphism is a technique in which we can use a same name methods in different purpose. It is used to change the statement of methods at runtime. We can achieve polymorphism in two ways i.e. first is compile time polymorphism and other is runtime polymorphism.
Types of polymorphism in c#-
In c#, there are two types of polymorphism,
1- Method overloading or Compile time polymorphism
2- method overriding or runtime polymorphism
Method Overloading: In this type of polymorphism there are many methods are used in a single class with same name and with different parameters. The difference between parameters is sequence, or types of parameters or the number of parameters are different. All methods are called by an instance of the same class and passes the arguments as we pass the parameters then it automatically call that exact methods.
Ex- public class methodOverloading { public void Test(String name) { Console.WriteLine('Your name is'+name); } public void Test(int mark) { Console.WriteLine('Your mark is'+mark);
} public void Test(String name, int mark) { Console.WriteLine('Your name is '+name+' and your mark is '+mark);
} } class Program { static void Main(string[] arg) { methodOverloading over=new methodOverloading(); over.Test('Ashu'); //it will call Test(string name) method. over.Test(87); // it will call Test(int mark) method over.Test('Ashu',87); //it will call Test(string name, int mark) method } }
MethodOverriding: This type of polymorphism is also called runtime polymorphism. In this, there are also all methods name are same but it declare in different classes. The parameters which is passes in all methods is same (Sequence of parameters are same, types of parameters are same and number of parameters are same). To execute a specific methods it will call its instance of class.
Ex.-
public class Examp { public void Task(string name, int id) { Console.WriteLine('Name: '+name +' Id: '+ id);
}
}
public class Exp { public void Task(string name, int id) { Console.WriteLine('Name: '+name + 'Id: '+id);
}
}
public class Mark { public void Task(string name, int id) { Console.WriteLine('Name: '+name + 'Id: '+ id);
}
}
class OverridigExp { static void Main(string[] arg) { Examp exe = new Examp();
exe.Task('Examp',001);
Exp exp = new Exp();
exp.Task('Exp ' ,002);
Mark mark = new Mark();
mark.Task('Mark ', 003);
} }
Liked By
Write Answer
What is polymorphism and how many types of polymorphism?
Join MindStick Community
You have need login or register for voting of answers or question.
Ashutosh Kumar Verma
27-Aug-2021Polymorphism : The word 'Polymorphism ' means 'many form' so Polymorphism is a technique in which we can use a same name methods in different purpose. It is used to change the statement of methods at runtime. We can achieve polymorphism in two ways i.e. first is compile time polymorphism and other is runtime polymorphism.
Types of polymorphism in c#-
In c#, there are two types of polymorphism,
1- Method overloading or Compile time polymorphism
2- method overriding or runtime polymorphism
Method Overloading: In this type of polymorphism there are many methods are used in a single class with same name and with different parameters. The difference between parameters is sequence, or types of parameters or the number of parameters are different. All methods are called by an instance of the same class and passes the arguments as we pass the parameters then it automatically call that exact methods.
Method Overriding: This type of polymorphism is also called runtime polymorphism. In this, there are also all methods name are same but it declare in different classes. The parameters which is passes in all methods is same (Sequence of parameters are same, types of parameters are same and number of parameters are same). To execute a specific methods it will call its instance of class.
Ex.-