Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same method name in parent and child class? [duplicate]

Tags:

c#

.net

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");

    }

}
like image 244
haansi Avatar asked Dec 27 '22 13:12

haansi


2 Answers

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"
    }
}
like image 162
Darin Dimitrov Avatar answered Dec 30 '22 02:12

Darin Dimitrov


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.

like image 33
Petar Ivanov Avatar answered Dec 30 '22 02:12

Petar Ivanov