Hi,
can anyone explain what is Overloading, Overriding and Hiding in .Net?
Thanks
Hi,
can anyone explain what is Overloading, Overriding and Hiding in .Net?
Thanks
http://en.wikipedia.org/wiki/Method_overriding_(programming)
Check the wiki page on OO: http://en.wikipedia.org/wiki/Object_oriented
Overloading is when you create multiple methods, with different parameters. For example:
public class Car { public Car() { // Do stuff } public Car(int numberOfCylinders) { // Do stuff } }
You can see the constructor has the same name, but different parameters. You will see overloaded methods and constructors in Visual Studio in Intellisense, with little arrows going up and down so you can browse the signatures.
Overriding is when you provide a new implementation to a method from a base class. Example:
public class Square { public double x; // Constructor: public Square(double x) { this.x = x; } public virtual double Area() { return x*x; } } class Cube: Square { // Constructor: public Cube(double x): base(x) { } // Calling the Area base method: public override double Area() { return (6*(base.Area())); } }
Note that in C#, you can not override a non-virtual or static method.
http://msdn.microsoft.com/en-us/library/ebca9ah3(VS.71).aspx
Overloading is providing multiple methods with the same name and different signatures:
void Foo() {...}
void Foo(int i) {...}
Overriding is used in inheritance to change the implementation per subclass:
class SomeBase {
public virtual void Foo() {}
}
class SomeOther : SomeBase {
public override void Foo() {
// different implementation, possibly calling base.Foo();
}
}
With the above, even if I do:
SomeBase obj = new SomeOther();
obj.Foo();
it will call the SomeOther.Foo
implementation. The method called depends on the instance type, not the variable.
Method hiding is the opposite; it replaces the method signature, but only applies if you call the method from the derived type:
class SomeBase {
public void Foo() {}
}
class SomeOther : SomeBase {
public new void Foo() {
// different implementation, possibly calling base.Foo();
}
}
Now:
SomeOther obj = new SomeOther();
SomeBase baseObj = obj;
obj.Foo(); // calls SomeOther.Foo
baseObj.Foo(); // calls SomeBase.Foo
i.e. the method calls depends on the variable, not the instance.
Overloading is the definition of multiple possible "signatures" of a single method or operator. Each signature takes different arguments, and is essentially a distinct function, no different than if the multiple functions had different names. This is often used to group conceptually similar operations, such as overloading +
to work with BigInteger
and with String
: both operations seem sensible to use +
for (unless you think that all overloads of + should define Abelian groups -- the String
overload doesn't).
Overriding is the definition of multiple possible implementations of the same method signature, such that the implementation is determined by the runtime type of the zeroth argument (generally identified by the name this
in C#).
Hiding is the definition of a method in a derived type with a signature identical to that in one of its base types without overriding.
The practical difference between overriding and hiding is as follows:
this
.this
.In simple terms:
Overloading is when you have same method name with different signatures. This can be in the same class. Eg:
int Add (int a, int b)
{
return a + b; // returns int
}
float Add (float a, float b)
{
return a + b; // returns float
}
Overriding is when you have the same method name and signature in the derived class as in the base class. In this scenario we say the derived class overrides the base class method. Which method runs depends on which instance calls the method
e.g.
public class Shape
{
public virtual void Draw() { }
}
public class Square : Shape
{
public override void Draw()
{
// Code to draw square
}
}
public class Circle : Shape
{
public override void Draw()
{
// Code to draw circle
}
}
Hiding or encapsulation is when you declare member variables and methods of a class private so that they cannot be accessed by any other class, not even the derived classes
E.g.
private int onlyICanAccess;
private void OnlyICanExecute()
{
// ...
}
Overloading is an example of poly morphsm, you can overloa number,datatype and sequence of parameter has been passed between two methods..
overriding(diferent from virtual) means some defined functionality are there,,but you can give more clear idea at the time of inheritind
Hiding means hide the internal features and represent essential features,one of the feature of OOP concept
GOOGLe to get detaied informations
Assuming that you understand the explanations given by other posters, one way to remember the difference between overloading and overriding is that, when using over**load**ing, there are potentially 'loads' of functions with the same name.
Silly, I know, but it works for me.