Polymorphism means that the same operation acts on instances of different classes and different classes are interpreted to produce different execution results.
Polymorphism is implemented by using a derived class to override the virtual function method in the base class.
There are two types of polymorphism:
Polymorphism during compilation (static polymorphism)
Runtime polymorphism (Dynamic polymorphism)
Implementation of polymorphism:
Interface Polymorphism
Multiple classes can implement the same "interface", while a single class can implement one or more interfaces. The methods, attributes, and events that the interface description class needs to implement, and the parameter types that each member needs to receive and return. specific implementations of these members are implemented by the Implementation class.
Realize polymorphism through inheritance
Multiple classes can be inherited from a single base class. The class receives all methods, attributes, and events of the base class in the implementation of the base class. C # provide polymorphism through inheritance
Implement polymorphism through abstract classes
The abstract class itself cannot be instantiated and must be inherited. Its implementation is implemented by the derived class,
Class Program
{
Static void Main (string [] args)
{
Employee base1 = new Employee ();
Base1.CalculatePlay ();
Employee base2 = new SalariedEmployee (); // Polymorphism
Base2.CalculatePlay ();
EalariedEmployee Ebase = new SalariedEmployee (); // Polymorphism
Ebase. CalculatePlay ();
SalariedEmployee base3 = new SalariedEmployee ();
Base3.CalculatePlay ();
}
}
Public class Employee
{
Virtual public void CalculatePlay ()
{
Console. WriteLine ("Employee ");
}
}
Class SalariedEmployee: Employee
{
Public override void CalculatePlay ()
{
Console. WriteLine ("Salary ");
}
Public void ss ()
{
Console. WriteLine ("111 ");
}
}
Class EalariedEmployee: Employee
{
Public override void CalculatePlay ()
{
Console. WriteLine ("Ealaried ");
}
}