Total Page Preview:   000000016160

What is Polymorphism in OOPS

Polymorphism means one name many forms."Poly" means many and "morph" means forms hence the name polymorphism.
One function behaves different forms.In other words, "Many forms of a single object is called Polymorphism."
 
Two Types of Polymorphism:-
 1:- Static Polymorphism (Compile Time Polymorphism)
 2:- Dynamic Polymorphism (Run Time Polymorphism)
 
Static Polymorphism (Compile Time Polymorphism):-
Static polymorphism is object during compile time is called early binding. It is also called static binding. There are two type of static polymorphism:
  I :- Method Overloading
  II :- Operator OverLoading
 
  Method Overloading :-
Function Overloading same method name with different type of parameters. You cannot overload function declarations that differ only by return type.
Example :-
public class Calculation
{
        //Method overloading here because both method name are same
        //This method will take 2 parameter 
        public int Add(int num1, int num2)
        {
            return num1 + num2;
        }
        //This method will take 3 parameter 
        public double Add(int num1, int num2, int num3)
        {
            return num1 + num2 + num3;
        }
 }
Dynamic Polymorphism (Run Time Polymorphism):-
Dynamic polymorphism also called as late binding or method overriding or Run time polymorphism. Method overriding means same method names with same signatures.
Dynamic polymorphism is implemented by abstract classes and virtual functions.
Abstract classes contain abstract methods, which are implemented by the derived class.The derived classes have more specialized functionality.
Example :
namespace ConsoleApplication1
{
    //Abstract Calss
    abstract class Shape
    {
        //Abstract Method in abstract class
        public abstract int Area();
        //Virtual Method in abstract class
        public virtual void Sample()
        {
            Console.WriteLine("Base Class");
        }
    }
    //Derived Class 
    class Rectangle : Shape
    {
        private int length;
        private int width;
        public Rectangle(int a = 0, int b = 0)
        {
            length = a;
            width = b;
        }
        //Abstract Method override in Derived Class
        public override int Area()
        {
            Console.WriteLine("Rectangle class area :");
            return (width * length);
        }
        //Virtual Method override in Derived Class
        public override void Sample()
        {
            Console.WriteLine("Derived Class");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Create object Rectangle Class
            Rectangle objRectangle = new Rectangle(10, 7);
            //Call Abstract Method
            double a = objRectangle.Area();
            Console.WriteLine("Area: {0}", a);
            //Call Virtual Method
            objRectangle.Sample();
            Console.ReadKey();
        }
    }
}
 

 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments

Vipin Mishra
06-Aug-2016
Very nice article

                           
                           

                           

                           

Facebook User: