public int Add(int a, int b)
public float Add(int a, int b)
returned type is different.
is this overloading or overriding ?
and what if access type is different
public int Add(int a, int b)
private int Add(int a, int b)
Can you help me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s neither: it’s invalid code. You can’t create two methods which differ only by return type. (Or by return type and access modifier…)
If you could, it would be overloading though – overriding happens across the inheritance hierarchy, where a method in a derived class overrides a virtual method declared in a base class. In this case, you’ve given no indication that they’re in different classes.
The biggest difference between overloading and overriding is when the decision is made about which method to use: the compiler picks the signature based on the compile-time types, and then the implementation is chosen based on the execution-time type of the actual object.
So if method
Derived.M(int)overridesBase.M(int), then the compiler doesn’t care – it just knows it’s callingBase.M(int)and lets the CLR take care of the virtual dispatch.However, if there are methods
Foo.M(int)andFoo.M(float)then the compiler decides at compile-time which of those will be used. (Of course, they could be virtual with overrides involved as well.)It’s worth noting that overloading across an inheritance hierarchy can be interesting, too – I’ve got a whole article about overloading which lists some of the oddities you might come across.