What is difference between overloading and overriding.
+20
A:
Overloading
Overloading is when you have multiple methods in the same scope, with the same name but different signatures.
//Overloading
public class test
{
public void getStuff(int id)
{}
public void getStuff(string name)
{}
}
Overriding
Overriding is a principle that allows you to change the functionality of a method in a child class.
//Overriding
public class test
{
public virtual getStuff(int id)
{
//Get stuff default location
}
}
public class test2 : test
{
public override getStuff(int id)
{
//base.getStuff(id);
//or - Get stuff new location
}
}
cgreeno
2009-03-23 15:01:54
+2
A:
- Overloading = Multiple method signatures, same method name
- Overriding = Same method signature (declared virtual), implemented in sub classes
An astute interviewer would have followed up with:
Michael Meadows
2009-03-23 15:07:04
Never knew it was called shadowing, good to know. The only relation overloading and overriding have is overing.
Samuel
2009-03-23 15:11:16
+2
A:
As Michael said:
- Overloading = Multiple method signatures, same method name
- Overriding = Same method signature (declared virtual), implemented in sub classes
and
- Shadowing = If treated as DerivedClass it used derived method, if as BaseClass it uses base method.
Migol
2009-03-23 15:10:08