10 July 2016 Sudhanshu Ranjan 910

What is the Difference between Overriding and overloading

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.

Points to be remember :

Overriding

  1. Methods name and signatures must be same.
  2. Method should be public.
  3. Method should have same data type.
  4. It needs inheritance.
  5. When a function of base class is re-defined in the derived class called as Overriding.
  6. Overriding is the concept of runtime polymorphism

Overloading

  1. It should have same method name with different Signatures.
  2. Method can be different access specifiers.
  3. Method can have different data types
  4. It doesn't need inheritance.
  5. Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading.
  6. Overloading is the concept of compile time polymorphism.

Overriding Example :

class Car
{
    public int SpeedLimit() 
    {
        return 150;
    }
}
class Ford : Car
{
    public int SpeedLimit()
    {
        return 180;
    }
    public static void main(String args[])
    {
    	Car obj = new Ford();
    	int num= obj.SpeedLimit();
    	Console.WriteLine("Speed Limit is:{0}",num);
    }
}

Overloading Example :

class Sum
{
    int add(int num1, int num2) 
    {
        return num1 + num2;
    }
    int add(int num1, int num2, int num3) 
    {
        return num1 + num2 + num3;
    }
}

 

You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2017 dotnetcube.com. All rights reserved