interviews.dotnetthread.com

↑ Grab this Headline Animator

Friday, April 9, 2010

Difference Between Function Overloading and Overriding ?

Overloading means having functions with the same name but with different signature.Signature includes method name and Parameters. These functions can be part of base class or derived class.

Whereas Overriding means changing the functionality of a method without changing the signature. We can override a funtion in base class by creating a similar function in derived class and by use virtual/override keywords.

Base class method has to be marked with virtual keyword and we can override it in derived class using override keyword.

Derived class method will completly overrides base class method i.e when we refer base class object created by casting derived class object a method in derived class will be called.

Example:

Base Class:
-------------------------------
public class BaseClass
{
public virtual void Method1()
{
Print("Base Class Method");
}
}

Derived class
---------------
public class DerivedClass: BaseClass
{
public override void Method1()
{
Print("Derived Class Method");
}
}

Usage
--------------------------
public class Sample
{
public void TestMethod()
{
DerivedClass objDC = new DerivedClass();
objDC.Method1();
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}

Result
---------------------
Derived Class Method
Derived Class Method

Submit this story to DotNetKicks

2 comments:

.NET Application development said...

I constantly similar to interpret amazing love this.Thanks for this post.I have clearly in Difference Between Function Overloading and Overriding.

Anonymous said...

thanx alot for ur post man..........it really helps me alot..keep posting different topics...........from raghu

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles