Polymorphism means ability to have many forms. There are two types pf polymorphism.
- Compile time polymorphism/Overloading
- Runtime polymorphism/Overriding
Overloading
- Compile time polymorphism is method and operators overloading. It is also called early binding.
- In method overloading method performs the different task at the different type and different no of input parameters with the same method name.
- You should consider overloading a method when you need a couple of methods for some reason that take different parameters, but conceptually do the same thing.
class Program { public class Shape { public void Area(float r) { float a = (float)3.14 * r;
// Function overload with 1 parameter.
Console.WriteLine("Area of a circle: {0}",a); } public void Area(float l, float b) { float x = (float)l* b;
// Function overload with 2 parameters.
Console.WriteLine("Area of a rectangle: {0}",x); } public void Area(float a, float b, float c) { float s = (float)(a*b*c)/2;// Function overload with 3 parameters.
Console.WriteLine("Area of a circle: {0}", s); } } static void Main(string[] args) { Shape shape= new Shape(); shape.Area(2.0f);
shape.Area(20.0f,30.0f);
shape.Area(2.0f,3.0f,4.0f);
Console.ReadLine(); } }
Overriding
- Method overriding means changing the implementation of an inherited method.
- If a declare a method as virtual in the base class, we can override it in a derived class.
- At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method.
- In the source code you can call a method on a base class, and cause a derived class's version of the method to be executed.
- Virtual methods enable you to work with groups of related objects in a uniform way. For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. Even though this can be achieved by no of switch statements, it will be hard to maintenance. So you can achieve the same thing by using overriding a method.
public class Shape { public int X { get; private set; } public int Y { get; private set; } public int Height { get; set; } public int Width { get; set; }
// Virtual method
public virtual void Draw() { Console.WriteLine("Performing base class drawing tasks"); } } public class Circle : Shape { //Override Method public override void Draw() { // Code to draw a circle... Console.WriteLine("Drawing a circle"); } } public class Rectangle : Shape { public override void Draw() { // Code to draw a rectangle... Console.WriteLine("Drawing a rectangle"); } } public class Triangle : Shape { public override void Draw() { // Code to draw a triangle... Console.WriteLine("Drawing a triangle"); } } public class Program { static void Main(string[] args) { var shapes = new List
{ new Rectangle(), new Triangle(), new Circle() }; // invoked Draw() on each of the derived classes, not the base class. foreach (var shape in shapes) { shape.Draw(); } Console.Read(); } } Output Drawing a rectangle Drawing a triangle Drawing a circle
public class Triangle : Shape
{
public sealed void Draw()
{
// Code to draw a triangle...
Console.WriteLine("Drawing a triangle");
}
}
public class Triangle : Shape
{
public override void Draw()
{
base.Draw();
}
}
No comments:
Post a Comment