Saturday, August 9, 2008

POLYMORPHISM, COMPILATION POLYMORPHISM AND RUNTIME POLYMORPHISM

Polymorphism is the core concept of OOP's. Polymorphism means one name many forms.



Types of polymorphism



  1. Compile Time Polymorphism

  2. Run Time Polymorphism



Compile Time Polymorphism


The compile time polymorphism, here the polymorphism is implemented during compile time, that means at the time of compilation the compiler knows where to bind the method. The compile time polymorphism can be implemented through:



  • Method Overloading

  • Operator Overloading



Method Overloading:


In complex applications written in C#, we may need many methods which do essentially similar functions but are just different enough to be considered unique. So, we can define method overloading as if two or three method in a class has same name but they differ by number of arguments or type of argument.



Operator Overloading:


This provides a meaningful understanding of the operation by using operator overloading. Here what we did is we overload an operator and change its meaning, so that a valuable information is send to the programmer and this help in reducing the complexity.


E.g.


If we need to add two matrix of 3X3


Matrix result = Matrix.Add(mat1, mat2); // this doesn't give any relevant information



Run Time Polymorphism


The run time polymorphism, here the compiler doesn't know which method to call at runtime. Here we can call the derived method through base class pointer at runtime. The run time polymorphism can be implemented through: Virtual – Override Keyword.



Vitual function:


The virtual function is used to implement runtime polymorphism; here we have same name method in the base class as well as in the derived class. We can access the derived method using the base pointer. The virtual keyword should be write in front of the base class method and in the derived class we have to write override in front of the method. For Example:-



Class BaseClass {


Virtual void display () {//statements}


}


Class DerivedClass {


Override void display () {//statements}


}



Example: Demonstrate Polymorphism






BaseClass8.cs



using System;


using System.Collections.Generic;


using System.Text;



namespace cSHARPEXAMPLES


{


class BaseClass8


{


String str = "Base" ;


public BaseClass8() { }


public String display() {


return str + ": BaseClass Display" ;


}


public virtual String show() {


return str + ": BaseClass Show" ;


}


}


}


DerivedClass8.cs



using System;


using System.Collections.Generic;


using System.Text;



namespace cSHARPEXAMPLES


{


class DerivedClass8 : BaseClass8


{


String str = "Derived" ;


public DerivedClass8() { }


public new String display() { //NEW KEYWORD is USE TO HIDE THE


//SAME METHOD IN TH BASE CLASS


return str + ": Display Derived" ;


}


public override String show() { //OVERRIDE KEYWORD IS USED IF


//THE METHOD IN THE BASE CLASS IS VIRTUAL


return str + ": SHOW Derived:" ;


}


}


}



Form8.cs




using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;



namespace cSHARPEXAMPLES


{


public partial class Form8 : Form


{


public Form8()


{


InitializeComponent();


}


private void button1_Click( object sender, EventArgs e)


{ //Virtual: Polymorphism at RunTime


BaseClass8 objBase = new DerivedClass8 ();


label5.Text = objBase.display();


label6.Text = objBase.show();


}


private void button2_Click( object sender, EventArgs e)


{


BaseClass8 objBase = new BaseClass8 ();


label1.Text = objBase.display();


label2.Text = objBase.show();


}


private void button3_Click( object sender, EventArgs e)


{


DerivedClass8 objDer = new DerivedClass8 ();


label3.Text = objDer.display();


label4.Text = objDer.show();


}


}


}



Output



No comments:

Docker Tutorial

 I have listed all the necessary commands of Docker below. In case if any is missing or if any improvement required, please share in comment...