Wednesday, September 30, 2015

What is polymorphism?

Polymorphism means many form. ploy means multiple and morph means forms.
in ploymorphism we declare methods with same name and different  parameters in same class or method with same name and same parameters in different classes.

two type of ploymorphism
compile time ploymorphism, (pr early binding, overloading ,static binding)
Run time ploymorphism (or late binding, overriding, dynamic binding)

Method Overloading
              Method overloading is used to increase the readability of program method overloading is perform with in class.
Method overloading performed within class.
in case of method overloading parameter must be different. Method overloading is the example of compile time polymorphism.method overloading cant be performed by changing return type of the method only. Retun type can be same or different in method overloading.but you must have to change the parameter.

Method overriding
              Method overriding is used to provide the specific implementation of the method that is already providing by its super class.
 Method overriding occurs in two classes that have IS-A (inheritance) Relationship.
in Class of method overriding parameter must be same.
Method overriding is example of run time ploymorphism.

Compile time Polymorphism

Public class Class1
{
Public void NumbersAdd(int a,int b)
{
Console.write(a+b);
 }

Public void NumbersAdd(int a , int b , int c)
{
Console.write(a+b+c);
}

}

Run Time Polymorphism

Public class Bclass
{
Public virtual void Sample1()
{
Console.write(“Base classs”);
} }

Public class Dclass:Bclass
{
Public override void Sample1()
{
Console.write(“Derived class ”);
}
}

Class Program
{
Static void main (string [] args)
{
DclassobjDC=new Dclass();
objDc.sample1();
BclassobjBC= new Dclass();
objBc.Sample1();
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.