Implements of polymorphism in c#


polymorphism allows you to define method of a base class with different functionality but same name ,in derived classes.in this code we will show how to implements polymorphism in c#. in this code class2 that contains methods, View1() and View2(),with the same name as the method in class1 class.

Polymorphism allows to define method of a base class with different functionality but same name,in drieved class.
For example there is a base class shape,which contain the method calcarea(). we can defined a method with name clacarea in derived classes,square,rectangle and circle.The calcarea() method in the derived classes contain different functionality.
The calcarea() method in square class contains th functionality for calculating the area of a square.
Thecalcarea() method in rectangle class contained the functionality for creating the area of a rectangle.
The following code shows how to implements Polymorphism in c#.


using System;
using system.Collections.Generic;
using System.Text;

namespace CsharpPolymorphism
{

class class1
{
public void View()
{
Console.WriteLine("class1.View1()being called");
}

public virtual void View2()
{
Console.Writeline("class1.view2() being called");
}
}

class class2 : class1
{
public new void View()
{
Console.WriteLine("class.View1() being called.");
}
public override void View2()
{
Console.Writeline("class2.View2()being called");
}
}
class program
{
static void main(string[]args)
{
class obj=new class2();
obj1.View();
obj1.View2();
}
}
}


in this code the class2 class contains method,View1() and View2(),with the same name as the method in class1 class.The class2 class is a derived class and class1 class is the base class.

This program output will be:
class1.View1() being called.
class2.View() being called.
press any key to continue ...


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: