I had an interview where I was asked the following:
Question: A method with same name and signature but different return type. Is it possible and what is this type called he asked me.
Can someone please tell me the following:
Is above thing possible in any scenarios (Like one in base class and one in derived class atleast ?) If so what type is it ? Like compile or runtime polymorphism ?
In compile time polymorphism, what if the return types of methods are also different along with signature ? But only the name of function is same. Is it compile time polymorphism still ?
In overriding, what if I have different return type but the method name and signature are same ? Is it possible ? (He asked me this question, I answered wronglY :() Please help me.
Thank you
We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.
No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4. 2) methods with same signature is not allowed in this case.
It allows a method to return a more-derived type than the one declared in a base type e.g.
public interface ISomeInterface
{
object GetValue();
}
public class SomeClass : ISomeInterface
{
public string GetValue() { return "Hello world"; }
}
This is supported in Java but not in C#. The above will not compile since the return type of SomeClass.GetValue
is string
not object
.
Note that you cannot overload methods based on return-type alone i.e. the following is not valid:
public class SomeClass
{
public int GetValue() { return 1; }
public string GetValue() { return "abc"; }
}
You could do something similar using interfaces although you would need to implement them explicitly to disambiguate:
public interface IValue<T> { T GetValue(); }
public class SomeClass : IValue<int>, IValue<string>
{
string IValue<string>.GetValue() { return "abc"; }
int IValue<int>.GetValue() { return 1; }
}
If the names are the same but the parameters are different then this is method overloading. This is a form of polymorphism (ad-hoc polymorphism). Overloads are resolved statically at compile-type (unless you're using dynamic
in which case they are deferred to run-time).
You can overload on both the number of parameters and their type, so the following are all valid:
public void DoSomething(int value) { }
public void DoSomething(string value) { }
public void DoSomething(int value, string value) { }
Note that you can vary the return type of these methods - methods cannot only be overloaded based on their return type alone but they can differ if their parameter lists are different.
Again this is return type covariance and is not supported in C#.
In C#, you cannot have methods like
int Foo() { return 1; }
void Foo() { return; }
They must vary by more than return type.
If the arguments are different, then you're good to go.
int Foo(string x) { return 1; }
void Foo(double x) { return; }
Though return type covariance is not supported in C#, it is possible to emulate it by using explicit implementation and method hiding. That is a pattern which is used thoroughly in the ADO.NET APIs.
E.g.:
public interface ISomeValue { }
public abstract class SomeValueBase : ISomeValue { }
public class SomeValueImpl : SomeValueBase { }
public interface ISomeObject { ISomeValue GetValue(); }
public abstract class SomeObjectBase : ISomeObject
{
ISomeValue ISomeObject.GetValue() { return GetValue(); }
public SomeValueBase GetValue() { return GetValueImpl(); }
protected abstract SomeValueBase GetValueImpl();
}
public class SomeObjectImpl : SomeObjectBase
{
protected override SomeValueBase GetValueImpl() { return GetValue(); }
public new SomeValueImpl GetValue() { return null; }
}
By doing that, the net result of calling GetValue()
is that it will always match the most specific available type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With