Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading,Overriding and Hiding? [closed]

Tags:

.net

oop

can anyone explain what is Overloading, Overriding and Hiding in .Net?

Thanks

like image 358
Bhaskar Avatar asked May 13 '09 06:05

Bhaskar


People also ask

What is Overloading & overriding?

Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.

What is the difference between overriding and hiding?

Hiding redefines the complete method, whereas overriding redefines only the implementation of the method. In Overriding, you can access the base class using the child class' object overridden method.. Shadowing has cannot access the child class methods.

What is the difference between overloading hiding shadowing and overriding in OOP?

Two methods with the same name and same class but the different signature is known as overloading and the method is known as an overloaded method while a method with the same name and same signature but in parent and child class is known as overriding.

What is the difference between method hiding and method overloading?

For Method overriding override keyword is being used. In case of Method Hiding new keyword is used to define new implementation in child class. In Method Overriding the implementation type of the method is of object type. However on other hand implementation type of method in Method hiding is of reference type.


2 Answers

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:

  • If a method is overridden, the implementation to call is based on the run-time type of the argument this.
  • If a method is simply hidden, the implementation to call is based on the compile-time type of the argument this.
like image 190
Jeffrey Hantin Avatar answered Oct 04 '22 05:10

Jeffrey Hantin


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.

like image 28
Marc Gravell Avatar answered Oct 04 '22 05:10

Marc Gravell