The C - Tree
A hub of C, C++ and C# programming Stuff.
Pages
Home
What is C#?
C & C++
Books and Training
About Me
Search (Article Or Program)
03 March 2013
Differences between method overloading and method overriding
Method Overloading and Method Overriding are both the techniques used to implement FUNCTIONAL POLYMORPHISM
They enhance the methods functionality using their respective features.
overloading:
1) It involves having another method with the same name in a class or its derived class.
2)can be implemented with or without inheritance.
3)It is implemented by:
a)changing the number of parameters in different methods with same name.
b)changing the parameter data types (if the number of parameters are same)
c)changing parameter order.
4)applicable to static as well as non static methods:
5)no keywords needed before the method names in C#.
6)also called as COMPILE TIME Polymorphism.
example: int add(int a, int b)
{
return a+b;
}
int add(int a)
{
return a*5;
}
Overriding:
1) It involves having another method with the same name in a base class and derived class.
2)always needs inheritance.
3)It is implemented by having two methods with same name, same signature, but different implementations (coding)in a base class and derived class
4)applicable to nonstatic, nonprivate methods only.access modifiers of the methods are not changed in the base and derived classes.
5)virtual and override keywords are needed before the method names in base and derived classes.
For overriding in firther classes, override keyword will be used,
6)RUN TIME Polymorphism
example:
class A
{
public virtual void demo()
{
}
}
class B:A
{
public override void demo()
{
}
static void Main()
{
B obj=new B();
obj.demo();
}
}
Newer Post
Older Post
Home