Dec 28, 2019

POLYMORPHISM IN C#


polymorphism in c#, method overriding , difference between overloading and overriding , polymorphism definition,polymorphism example
polymorphism definition 

POLYMORPHISM

            The process of re-implementation of parent class member in child class with same name but different behavior, state with the help of keyword ‘virtual’ and ‘Override’.

RULE TO PERFORMING POLYMORPHISM

            We have to follow some rule to perform polymorphism.
       1.    Inheritance should develop between classes to perform polymorphism.
       2.    Parent class member should rewrite in child class with new behavior.
       3.    Which the member should call, constructor will decide, on the base of constructor the member of either child or parent will call.
       4.    For the permission sake parent class member should declare virtual like this,
Public class Parent
{
  Public virtual void member ( )
}
           When a member declare virtual its mean it override able  in its child class.
        5.    Child member should declare with keyword override like this,
Public class child: Parent
{
   Public override void member ( )
}

METHOD OVERRIDING

            The redefining of parent class methods in child class with same name with different behavior and state. The parent class should virtual and the class member should define override.

SYNTAX

Public class Parent
{
  Public virtual void Testmethod ( )
     {
       Task;
     }
}
Public class Child: Parent
{
  Public override void Testmethod ( )
  {
     Task;   //changed behavior
  }
}

CALLING METHOD SYNTAX

Parent obj = new child ( );
Obj. Testmethod ( );  // here the constructor if child is calling thus the Testmethod of  
                                     child will execute

      DIFFERENCE BETWEEN OVERLOADING AND OVERRIDING 


Overloading
1.    The method of defining more than one method with same name but different parameters.
2.    Overloading means the methods have different behavior for each method.
3.    Overloading can perform between child parent classes and also in a class.
4.    To perform overloading we can define same name of method in child class without permission of parent class.



Overriding
1.    The method of redefining a parent class method in child class with same name same parameter.
2.    Overriding means the change in behavior of parent method and child method.
3.    Overriding can perform in between parent child class only.
4.    To performing overriding its necessary to take permission of parent class.


0 comments:

Post a Comment

Please do not enter any spam link in the comment box