1

Why I am not able to access S class method? And why am I able to create the method with the same name in class M?

public class S
{
    public S()
    {
    }

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

public class M:S
{
    public M()
    {
    }

    public string myFunc(int a, int b)
    {
        return (a + b).ToString();
    }
} 

public class Test
{
    public static void Main(string[] args)   
    {
         M mm = new M();
         mm.myFunc(1,2);   // Why I am not able to access S class myFunc
    }
}
Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
Manish Kumar
  • 335
  • 1
  • 5
  • 16

7 Answers7

5

This is known as member hiding. You can access the S class method by casting the reference:

public class Test 
{ 
    public static void Main(string[] args)
    { 
        M mm = new M(); 
        string x = mm.myFunc(1,2);     // calls M.myFunc
        int y = ((S)mm).myFunc(1,2);   // calls S.myFunc

        S ss = new M();
        int z = ss.myFunc(1,2);        // calls S.myFunc
    } 
}

You should use the new modifier when defining the derived class member; otherwise, you will get a compiler warning.

Edit: Note that member hiding is different from polymorphism. In member hiding, the class member is resolved at compile-time based on the declared type of the reference. In my example above, ss is declared as S, even though it is actually assigned an instance of type M.

To achieve polymorphism, you need to specify the virtual modifier on your base class member, and override on your derived class member. Consequently, calls to virtual members are resolved at run-time to the actual type of the instance. However, polymorphism does not allow you to change the return type, so you cannot use it in your example.

Douglas
  • 53,759
  • 13
  • 140
  • 188
3

Because c# doesn't overload based on return type, only on name and parameters. So M overloads S and S's myFunc becomes unreachable. Either change the name or the parameters.

If you cast it to S you will lose the extra values that M has

matthijsb
  • 909
  • 5
  • 12
  • I could not understand this. Because First I am inheriting the S class so It's method should be available in class M and if it is available then why I am able to create the method with the same in in class M. – Manish Kumar Feb 26 '14 at 13:12
  • When you create a method with the same name and parameters, it automatically overloads it. e.g S' method becomes unreachable in M. It is hiding the method – matthijsb Feb 26 '14 at 13:33
0

Changing the return type is not overloading the method (you cant have two exactly same methods in one class with same name and parameter and different return type).

Reason is obvious - how to decide, which one will be called?

To overload something, you have to have different parameters.

libik
  • 22,239
  • 9
  • 44
  • 87
0

You'll be able to access it via base.myFunc(1,2);

RST_7
  • 86
  • 4
0

You need to cast the object like this:

 (S(mm)).myFunc(1, 2); // now you acces the int returns.

For good practice, if a method has a different return the other, they must have different names.

Only a Curious Mind
  • 2,807
  • 23
  • 39
0

Because

        public string myFunc(int a, int b)

is hiding

        public int myFunc(int a, int b)
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33
0

Well, in addition to what GrooV said, in C# you need to declare the base method as "virtual" and the method in the child class "override" so it can be properly overridden and not break polymorphism. Read these two pages, they will help you understand better:

override (C# Reference)

Is it possible to override a non-virtual method?

Community
  • 1
  • 1
Sari Alalem
  • 870
  • 7
  • 18