Monday, 5 March 2018

012 C# Polymorphism

Polymorphism
Polymorphism means one name with many forms. Polymorphism is a good feature of object oriented programming. Polymorphism is implemented with overloading function and operator. When a function is called, the compiler invoke the function by matching argument,type and number of parameters. If argument type number of parameters match then compiler invoke that function. When this information is available compile time, compiler know which function have to call. This is known as Compile Time Polymorphism or Static Binding. This is all also called as early binding .
But sometimes compiler does not know which function have to call during the compile time, detect the function during runtime, match the argument,type and number of parameters solo and select the appropriate function. Virtual function is an example of runtime polymorphism. This process is known as dynamic binding, late binding. 



Function Overloading : function loading is a feature of C#. When there is function of same name with different parameter and different implementation, this is called function overloading. A function has implementation in different invoke of the function. The execution of the correct implementation decided depending upon parameter type, number. 
 
public class Students
{
              public int GetFees()
              {
                  return 0;
              }
               public int GetFees(int rollNumber)
             {
                return 0;
             }
             public int GetFees(string Name,DateTime dob )
            {
            return 0;
           }
           public int GetFees(int registration_number,int Year_of_admission)
          {
           return 0;
          }
}
public class School
{
         public void Show()
         {
            Students obj = new Students();
            int fees;
            fees = obj.GetFees();
            fees = obj.GetFees(123);
            fees = obj.GetFees("John",DateTime.Today);
           fees = obj.GetFees(123,2020);
       }
}


Operator Overloading : C# has a number of operator. Operator has its own use. Operator feature can be changed by the implementation of the code. 
class Shudent
{
         double adminssion_fees, session_fees,exam_fees ,total_fees;
        public Shudent(double fee1, double fee2, double fee3)
         {
            adminssion_fees = fee1;
            session_fees = fee2;
             exam_fees = fee3;
        }
       public static Shudent operator ++(Shudent obj)
       {
         obj.total_fees = obj.adminssion_fees + obj.session_fees + obj.exam_fees;
         return obj;
       }
}

Virtual Function : Inheritance is a good feature of object oriented programming. The base class and derived class concept is the main features. While base class function is derived derived class and the derived class has the same name function, what happened in this situation when base class and derived class has same name function? The base class function, declare with keyword virtual will avoid the conflict who stop the virtual keyword tell the compiler that which function should we call will be decided at runtime. At the run time, which parameter and type, number match this function will be call. Are some topics related to virtual function. 
public class Teacher
{
           public virtual string GetHeight()
          {
           return "Base Class";
           }
}

public class Student : Teacher
{

        public override string GetHeight()
        {
         return "Function Override";
        }
        public string GetHeight(int param1, int param2)
       {
       return "Function Another Defination 1";
       }
       public string GetHeight(int param1, string param2)
       {
       return "Function Another Defination 1";
       }
}

public class Student
{
         public void Show()
        {
           Student obj = new Student();
          string height;
          height = obj.GetHeight();//Call base class function
          height = obj.GetHeight(11, 22);
         height = obj.GetHeight(11, "John");
         }
}
Output :
Function Override
Function Another Defination 1
Function Another Defination 1
  • Virtual function must be defined within a class 
  • A constructor cannot be virtual 
  • Virtual function cannot be static 
  • If a virtual function is defined in the base class, it is obvious the derived class.

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...