Feb 24, 2014

what is overloading and overriding

Overloading is defining functions that have similar signatures, yet have different parameters. 

Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that function.

Overriding
Overloading
Methods name and signatures must be same.
Having same method name with different
Signatures.
Overriding is the concept of runtime polymorphism
Overloading is the concept of compile time polymorphism
When a function of base class is re-defined in the derived class called as Overriding
Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading
It needs inheritance.
It doesn't need inheritance.
Method should have same data type.
Method can have different data types
Method should be public.
Method can be different access specifies

Example

Overriding

public class MyBaseClass
{
    public virtual void MyMethod()
    {
        Console.Write("My BaseClass Method");

    }
}
public class MyDerivedClass : MyBaseClass
{
    public override void MyMethod()
    {
        Console.Write("My DerivedClass Method");

    }
}

Overloading 

int add(int a, int b)
int add(float a , float b)

No comments:

Post a Comment