0
OOPS Polymorphism
Posted by Rajendra Prasad Panchati
on
Wednesday, February 17, 2010
Polymorphism means same operation may behave differently on different classes.
Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.
There Are Two Types of Polymorphism
1. Static Polymorphism
2. Dynamic Polymorphism
1. Static Polymorphism
In Static Polymorphism ,Which method is to be called is decided at compile-time only. Method Overloading is an example of Static Polymorphism.
Method overloading is a concept where we use the same method name many times in the same class,but different parameters. Depending on the parameters we pass, it is decided at compile-time only, which method is to calles. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permits. In Static Polymorphism decision is taken at compile time.
Method Overloading is an example of Compile Time Polymorphism.
public Class CompileTime
{
public void Calculate(int x)
{
Console.WriteLine("Area of a Square: "+x*x);
}
public void Calculate(int x, int y)
{
Console.WriteLine("Area of a Rectangle: "+x*y);
}
public static void main()
{
CompileTime objCompileTime=new CompileTime();
objCompileTime.Calculate(2);
objCompileTime.Calculate(2,3);
}
}
Output : Area of a Square: 4 and Area of a Rectangle: 6.
2. Dyanamic Polymorphism
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
In this Mechanism by which a call to an overridden function is resolved at a Run-Time( not at Compile-time). If a BaseClass contains a method that is overridden.
Method Overriding is an example of Run Time Polymorphism
Class Test
{
Public void Show()
{
Console.WriteLine("From base class");
}
}
Public Class RunTime: Test
{
Public void Show()
{
Console.WriteLine("From Derived Class");
}
Public static void main()
{
RunTime objRunTime=new RunTime();
objRunTime.Show();
}
}
Output : From Derived Class
Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.
There Are Two Types of Polymorphism
1. Static Polymorphism
2. Dynamic Polymorphism
1. Static Polymorphism
In Static Polymorphism ,Which method is to be called is decided at compile-time only. Method Overloading is an example of Static Polymorphism.
Method overloading is a concept where we use the same method name many times in the same class,but different parameters. Depending on the parameters we pass, it is decided at compile-time only, which method is to calles. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permits. In Static Polymorphism decision is taken at compile time.
Method Overloading is an example of Compile Time Polymorphism.
public Class CompileTime
{
public void Calculate(int x)
{
Console.WriteLine("Area of a Square: "+x*x);
}
public void Calculate(int x, int y)
{
Console.WriteLine("Area of a Rectangle: "+x*y);
}
public static void main()
{
CompileTime objCompileTime=new CompileTime();
objCompileTime.Calculate(2);
objCompileTime.Calculate(2,3);
}
}
Output : Area of a Square: 4 and Area of a Rectangle: 6.
2. Dyanamic Polymorphism
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
In this Mechanism by which a call to an overridden function is resolved at a Run-Time( not at Compile-time). If a BaseClass contains a method that is overridden.
Method Overriding is an example of Run Time Polymorphism
Class Test
{
Public void Show()
{
Console.WriteLine("From base class");
}
}
Public Class RunTime: Test
{
Public void Show()
{
Console.WriteLine("From Derived Class");
}
Public static void main()
{
RunTime objRunTime=new RunTime();
objRunTime.Show();
}
}
Output : From Derived Class
More
Less