1

Code:

class Program
{
    class A
    {
        public void abc(int x)
        {
            Console.WriteLine("abc from A");
        }
    }
    class B : A
    {
        public void abc(double x)
        {
            Console.WriteLine("abc from B");
        }
    }
    static void Main(string[] args)
    {
        B b = new B();
        b.abc(100);
        Console.ReadLine();
    }
}

On calling abc() method from class B instance, eventhough we are passing integer data ie 100 as an argument still why the execution of the program goes with method parameter that is defined with double? Please note that there is a method already defined for integer in the parent class.

Why the output of the above program is "abc from B" rather than "abc from A". Kindly advise.

Skaria Thomas
  • 419
  • 3
  • 10
  • 1
    Also, if you change your signature in the child class to, for example, string, parent method is called. 100 is a valid double so the function doing the hiding in child is called. – Sıddık Açıl Jun 20 '19 at 10:46

1 Answers1

0

Your methods are overloaded and You are observing that cause an similar resolution of method present in your class B which would be chosen first than a matching method from inherited class.

Rahul
  • 76,197
  • 13
  • 71
  • 125