Technical Articles

What is Polymorphism?

0 0
Behaving in different ways depending upon input receive is know as polymorphism. i.e. whenever input changes automatically output or behavior also changes.

There are basically two types of polymorphism in c# i.e.

Static/Compiletime/Early Binding 
Dynamic/Runtime/ Late Binding



Static- Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are overloaded  with same name but having different signatures.So it is called as method overloading.

 – In case of overloading methods whenever we call a method for that method call based on parameter if identifies which method should be executed and binds the method calls its method definition and method executed in runtime.

Dynamic-Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.

In Dynamic polymorphism methods are overridden so it also called as method overriding. 

During run time, Method overriding can be achieved by using inheritance principle and using “virtual” and “override” keyword. so this type of polymorphism can also be called as late binding.
In late binding compiler doesn’t know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.


Polymorphism can be implemented in object oriented programming language using three different approach:


1.Overloading

2.Overriding
3.Hiding/Shadowing

Overloading is again of 3 types-


  • Method Overloading
  • Constructor Overloading
  • Operator Overloading


Method Overloading:

  • Its an approach of defining multiple methods under a class with the same name by changing their signature.
  • changing the signature in the sense, we can change the numbers of parameter being passed to method , type of parameter being pass to the method , order of parameter.

Example:

public class Calculation

{


        public int Sum(int a, int b)

        {

            return a + b;

        }


        public float Sum(int xint yfloat z)

        {

            return x + y + z;

        }

        

}

Constructor Overloading:
  • Defining multiple constructor in a class with a different signature is known as constructor overloading i.e. just like we overload methods of a class , we can also overload constructor of a class.
  • If a class contains multiple constructors in it , object of that class can be created by calling any of available constructor it is not require to call a specific constructor for creating the object.

Example:


Note-   
  • Generally we overload constructor of a class so that variable of that class can be initialized either with default values with the help of parameter less constructor or can be initialized with the given values that are passed as parameter to the constructor while creating the object .
  • Parameter less constructor also known as default constructors, they will initialize variable of a class with the default value.   
Operator Overloading:
  • This is something very much similar to the concept of method overloading which allows to define multiple behaviors to an operator.
  • In method overloading the behavior of method changes according to the type of parameter being passed to the method , whereas in the operator overloading the behavior of an operator changes according to the operands types between which we use the operator.
  • ‘+’ is an overloaded operator which can be used both for addition as well as concatenation also. it works as a addition operator when used between numeric operands and works as a concatenation operator when used between string and numeric  operands.

Example: + is already overloaded operator under the base class.


Syntax: [<modifier>]static<type>operator<opt>(<oprand type def>)
            {
                 -stmnt-
            } 
  • <type> : implies the type of result we are expecting when the operator is used between two operands.
  • <operand type definition>: in the sense the type of operands between which we want to use the operator.
  • operator: is the name of the method that can naot be changed.
  • <opt>: iplies the operator we want to overload. 

Method Overriding:

  • If at all parent classes method is redifined or reimplemented under the child class , exactly with the same  name & signature , we call it as method overriding.
  • Method overriding is possible only in derived classes, but not within the same class. 
  • When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
  • To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
  • Method overriding in c# is achieved through inheritance.

Class1
public virtual void show() // overridable
class2: class1
public override void show() // overriding


Example 1:




Example 2:implementing abstract method

Method Hiding/Shadowing:

  • This is another approach that is used for re-implementing a parent class method under child class . A parent classes method can be re-implemented under its child class using 2 different approaches:- 1.overriding ,2. hiding/shadowing
  •  in the first case we re implemented the parent class method that are declared as virtual under the child class by using override modifier wherese in second case  we remplemented the parent classes method under child class even if they not declared as virtual ie. re implementation being performing without parent classes permission.

Example:





Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?