Skip to main content

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



Comments

Popular posts from this blog

The sign-in method you're trying to use isn't allowed. For more info, contact your network administrator

Above problem occur when you have not added a group (of which User try to login)  in your local policy login. To resolve the issue, follow the steps,
Log in as the administrator on the server, then start the Group Policy Management Editor by running the  gpmc.msc command from PowerShell or the Command Line window. 1. In the Group Policy Management window on the left hand side, select Group Policy Management. 2. Click to expand the  Forest tree node.
3. Click Domains.
4. Select your domain name.
5. Click Group Policy Objects. 6. In the right-hand window, double-click Default Domain Controllers Policy.
7. Right-click Default Domain Controllers Policy and Select Edit.
8. In the Group Policy Management Editor window, click Default Domain Controllers Policy.
9. Click Computer Configuration, and then click Policies.
10. Click Windows Settings, and then click Security Settings. 11. Click Local Policies and then click User Rights Assignments.
12. In the right pane, click Allow log on locally. 12. Cli…

ORA-38104: Columns referenced in the ON Clause cannot be updated

In my case, this error pertains to Oracle Merge query. After 30 minutes of time wastage and a lot of hunting on different forums, I come to know that where and why was my merge query throwing exception.

I was giving multiple criteria in ON clause of merge query, and was also trying to update a column which I have mentioned in ON clause. Once I remove that column from update clause of merge query, it executed successfully.

I hope that this will also works for you.

In case if this post help you, then please comment on it.

Thanks.

Cannot use the special principal 'sa'. (Microsoft SQL Server, Error: 15405)

Yesterday, I start SQL Server 2005 and try to connect with sa user but was enable to connect with the 'sa' user and got this error:

Cannot use the special principal 'sa' (Microsoft SQL Server, Error: 15405)After doing some google, I found out the solution which I am sharing so that others can get all the possible solutions from this article.
The titled message mean that your sa user is disabled. To enable it, you have to follow below mentioned steps:1) you have to change the authentication mode: go to Management Studio and open Object Explorer. Connect to your server and right-click on the server name, then select Properties. Go to Security tab and under Server Authentication select "SQL Server and Windows Authentication Mode".2) Also, after you follow the directions above, you will also need to explicitly enable the sa account as well. Go to Security->Logins. Right-click on the "sa" account, select "Properties". In the list on the lef…