Question

I am wondering that why the following scenario is not working in c#.

class a
{
    public  int add(int a, int b)
    {
        return a + b;
    }

    public string add(int c, int d)
    {
         return "String : "+(c + d).ToString();
    }
}

Since the following is working fine (in the case of inheritance)

class a
{
    public  int add(int a, int b)
    {
        return a + b;
    }
}

class b : a
{
    public string add(int c, int d)
    {
        return "String : "+(c + d).ToString();

    }
}

Here if I am creating an object of b and b.add(1,2) returns String:3

n both case two add method having different return type ( am aware that against method overloading) , but it working fine in the case of inheritance but not in single class.

Anybody knows the reason?

Was it helpful?

Solution 2

In the first example (both methods within one single class), no overloading can take place as the signatures of both methods are the same (only name and parameter types count, return value is ignored). This results in a compiler error.

In the second example, the method add with return type string hides the inherited method. When you have something like

a myB = new b();
myB.add(1,2);

Then the implementation of the base class will be called even if the actual type is b. For

b myB = new b();
myB.add(3,4);

the implementation of b gets called (look at the variable type of myB, it is important!).

OTHER TIPS

Your 2 add methods in class a are totally ambiguous. The name and the arguments are exactly the same.

Tell me, what should this do (other than not compile)?

int k = a.add(2, 3); // which one should it call? the string or int return type?

The compiler can't figure it out either.

The method is ambiguous. You can't define same parameters using the same method name.

but it working fine in the case of inheritance but not in single class.

This is not true. Your second case hides the public int add(int a, int b) implementation.

I suggest that's because of ambiguous behavior. Let's imagine a code:

class a {
  public  int add(int a, int b) {...}
  public string add(int c, int d) {...}
}

a test = new a();

// What method should be executed? 
// int add(1, 2) or string add(1, 2)?   
a.add(1, 2);

When you have different classes say a and b there's no such ambiguous behavior

let's say you are calling the method in the following way:

a.add(1,2);

Which one of the methods did you call?
If you can't answer that, you can be sure that neither can the compiler...

This works using inheritance since b.add 'hides' a.add.
The compiler will now which method to call by the context.
i.e.

b instance = new b();
b.add(1,2);

Will definitely call b's method. no ambiguity there... But still this is not a good practice and you will get a warning of: b.add(int,int) hides inherited member use the new keyword if hiding was intended

Let's understand function execution when we call any function the definition part executes first (int sum = i + y )and at last we require the return statement,hence we can say return comes after the function's whole definition.

That's why if there are two or more functions with same name and with same type and no. of arguments then at the time of calling how compiler will know about which one to be called, because function name and parameters are the same.

<code>
 public int MyFun(int i, int y)
 {
   int sum = i + y
   return sum;
 }
 public string MyFun(int i,int y)
 {
   return "";
 }
</code>

At the time of calling firstly all the focus will be on arguments and function name and after the completion of function definition at last we deal with return statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top