Does return type matter in overriding in C#?

Does return type matter in overriding in C#?

Yes. It is possible for overridden methods to have different return type . But the limitations are that the overridden method must have a return type that is more specific type of the return type of the actual method.

Can interface methods have return type in C#?

If an interface is defined to be the return type of a method then instances of classes derived from that interface can be returned. The benefit of doing that is no different from returning objects of classes derived from a class.

Can we change return type in method overriding?

Java version 5.0 onwards it is possible to have different return types for an overriding method in the child class, but the child’s return type should be a subtype of the parent’s return type. The overriding method becomes variant with respect to return type.

Can we override non abstract method in C#?

No you can’t. You can only override a virtual method – see the MSDN here: In C#, derived classes can contain methods with the same name as base class methods.

Should return type be same in method overloading and overriding?

Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this). Argument list should be different while doing method overloading.

Can we overload method with different return type C#?

You cannot overload two functions with the only difference being a return type. C# uses the parameter list as the context. You cannot overload a method by only having different return types.

Can interface method have return type?

3 Answers. If the return type must be the type of the class that implements the interface, then what you want is called an F-bounded type: public interface A>{ public T b(); } public class C implements A{ public C b() { } } public class D implements A{ public D b() { } }

Can we overload method with different return type?

No, you cannot overload a method based on different return type but same argument type and number in java. different parameters (different type or, different number or both).

Can we override without virtual in C#?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method.

Can overloaded methods have different return types C#?

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 type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.

author

Back to Top