Latest web development tutorials

C # polymorphism

Polymorphism means that there are multiple forms.In object-oriented programming paradigm, polymorphism is often expressed as "one interface, multiple functions."

Polymorphism can be static or dynamic.Static polymorphism in the response function occurs at compile time.Indynamic polymorphism, the response function occurs at runtime.

Static polymorphism

At compile time, the connection mechanism functions and objects is called early binding, also known as static binding. C # offers two techniques to implement static polymorphism. They are:

  • Function overloading
  • Operator Overloading

Operator overloading will discuss in the next section, we will discuss the following function overloading.

Function overloading

You can be in the same range for the same function name has multiple definitions. Defined functions must be different from each other can be a parameter list parameter types, can also be a number of different parameters. You can not be overloaded only by return type of the function declaration.

The following example demonstrates several of the same functionprint (), for printing different types of data:

using System;
namespace PolymorphismApplication
{
   class Printdata
   {
      void print (int i)
      {
         Console.WriteLine ( "Printing int: {0}", i);
      }

      void print (double f)
      {
         Console.WriteLine ( "Printing float: {0}", f);
      }

      void print (string s)
      {
         Console.WriteLine ( "Printing string: {0}", s);
      }
      static void Main (string [] args)
      {
         Printdata p = new Printdata ();
         // Invoke print to print an integer p.print (5);
         // Invoke print to print float p.print (500.263);
         // Invoke print to the print string p.print ( "Hello C ++");
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Printing int: 5
Printing float: 500.263
Printing string: Hello C ++

Dynamic polymorphism

C # allows you to use the keywordabstract to create an abstract class, interface implementation provides for part of the class.When a derived class inherits from the abstract class to achieve complete.Abstract class contains abstract methods, abstract methods can be implemented as a derived class.Derived classes have more specialized functions.

Please note that the following are some rules for abstract classes:

  • You can not create an instance of an abstract class.
  • You can not declare an abstract method outside an abstract class.
  • By placing in front of the class definition keywordssealed, the class can be declared as sealed classes.When a class is declaredsealed, it can not be inherited.An abstract class can not be declared as sealed.

The following program demonstrates an abstract class:

using System;
namespace PolymorphismApplication
{
   abstract class Shape
   {
      public abstract int area ();
   }
   class Rectangle: Shape
   {
      private int length;
      private int width;
      public Rectangle (int a = 0, int b = 0)
      {
         length = a;
         width = b;
      }
      public override int area ()
      { 
         Console.WriteLine ( "Rectangle class area:");
         return (width * length); 
      }
   }

   class RectangleTester
   {
      static void Main (string [] args)
      {
         Rectangle r = new Rectangle (10, 7);
         double a = r.area ();
         Console.WriteLine ( "Size: {0}", a);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Rectangle Class Size:
Area: 70

When a function is defined in the class needs to be implemented in a derived class, you can usevirtual methods.Virtual method is declared using the keywordvirtual.Virtual methods may be implemented differently in different inherited classes. Calls to virtual methods occurs at runtime.

Dynamic polymorphism is achieved throughabstract classes and virtual methods.

The following program demonstrates this point:

using System;
namespace PolymorphismApplication
{
   class Shape 
   {
      protected int width, height;
      public Shape (int a = 0, int b = 0)
      {
         width = a;
         height = b;
      }
      public virtual int area ()
      {
         Console.WriteLine ( "parent area:");
         return 0;
      }
   }
   class Rectangle: Shape
   {
      public Rectangle (int a = 0, int b = 0): base (a, b)
      {

      }
      public override int area ()
      {
         Console.WriteLine ( "Rectangle class area:");
         return (width * height); 
      }
   }
   class Triangle: Shape
   {
      public Triangle (int a = 0, int b = 0): base (a, b)
      {
      
      }
      public override int area ()
      {
         Console.WriteLine ( "Triangle class area:");
         return (width * height / 2); 
      }
   }
   class Caller
   {
      public void CallArea (Shape sh)
      {
         int a;
         a = sh.area ();
         Console.WriteLine ( "Size: {0}", a);
      }
   }  
   class Tester
   {
      
      static void Main (string [] args)
      {
         Caller c = new Caller ();
         Rectangle r = new Rectangle (10, 7);
         Triangle t = new Triangle (10, 5);
         c.CallArea (r);
         c.CallArea (t);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Rectangle Class Size:
Area: 70
Class Triangle area:
Area: 25