4

Possible Duplicate:
why do we need the new keyword and why is the default behavior to hide and not override?

I have a parent and a child class. Both have a method with the same name and compiler allow it. I couldn't understand it. Why compiler has not shown an error in child class if parent has a methoed with the same name. I am not using new virtual or override with methods. Kindly help me understanding this why compiler did not shows an error in child class ?

 class BaseClass
{

     public string SayHi()
    {

        return ("Hi");

    }

}

class DerivedClass : BaseClass
{

    public  string SayHi()
    {

        return (base.SayHi() + " from derived");

    }

}
Community
  • 1
  • 1
haansi
  • 5,470
  • 21
  • 63
  • 91

3 Answers3

12

The base method must be declared as virtual if you want to override it in a child class:

class BaseClass
{
    public virtual string SayHi()
    {
        return ("Hi");
    }
}

class DerivedClass : BaseClass
{
    public override string SayHi()
    {
        return (base.SayHi() + " from derived");
    }
}

If the base method is not declared as virtual you will actually get a compiler warning telling you that you are trying to hide this base method. If this is your intent you need to use the new keyword:

class BaseClass
{
    public string SayHi()
    {
        return ("Hi");
    }
}

class DerivedClass : BaseClass
{
    public new string SayHi()
    {
        return (base.SayHi() + " from derived");
    }
}

UPDATE:

To better see the difference between the two take a look at the following examples.

The first one using a base virtual method which is overriden in the child class:

class BaseClass
{
    public virtual string SayHi()
    {
        return ("Hi");
    }
}

class DerivedClass : BaseClass
{
    public override string SayHi()
    {
        return (base.SayHi() + " from derived");
    }
}

class Program
{
    static void Main()
    {
        BaseClass d = new DerivedClass();
        // the child SayHi method is invoked
        Console.WriteLine(d.SayHi()); // prints "Hi from derived"
    }
}

The second hiding the base method:

class BaseClass
{
    public string SayHi()
    {
        return ("Hi");
    }
}

class DerivedClass : BaseClass
{
    public new string SayHi()
    {
        return (base.SayHi() + " from derived");
    }
}

class Program
{
    static void Main()
    {
        BaseClass d = new DerivedClass();
        // the base SayHi method is invoked => no polymorphism
        Console.WriteLine(d.SayHi()); // prints "Hi"
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    He knew that already, he is asking why it doesn't result in a compiler error – Tom Knapen Sep 24 '11 at 10:52
  • 1
    @Tom Knapen, it results in a compiler warning. There is no need to result in an error. – Darin Dimitrov Sep 24 '11 at 10:52
  • 2
    This explanation describes the problems completely. The OP did not mention having seen the warning. – Mark Sep 24 '11 at 10:55
  • @Darin Dimitrov, thanks for replying but I coldn't understand what will benefit of overridding a method ? If I override my parent class's method will be gone if I do just what I am doing same will happen. Please guide – haansi Sep 24 '11 at 10:55
  • 1
    @haansi, when you hide the base method, polymorphism no longer applies and it is the base method that is being invoked instead of the derived one. I have updated my answer to provide you 2 examples of that so that you can better understand the difference. – Darin Dimitrov Sep 24 '11 at 11:19
1

Since the method is not virtual, there is no problem to have the same name. Which one is called depends merely on the type of the reference.

BaseClass o = new DerivedClass();
o. SayHi();

will return "Hi".

If you declare the parent method a virtual, then you either have to override it in the subclass or make it new (which is the default). If you override it the above code will return "Hi from derived". If you make it new that is the same as if it was not virtual.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • lvanov thanks, can u plz guide what will differene in method overriding and doing what I did ? what I get is in both ways parent class method will be gone ? – haansi Sep 24 '11 at 10:56
0

It is called Name hiding. Please read MSDN article on - Hiding through inheritance

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186