Of course, this is possible, but to override the method, the method must be virtualor abstract, like any other appearance.
class Base
{
internal virtual void Foo()
{
Console.WriteLine("Foo from Base");
}
}
class Derived : Base
{
internal override void Foo()
{
Console.WriteLine("Foo from Derived");
}
}
When you use a keyword new, it calls the hiding method , which is not the same. If I write this:
class Base
{
internal void Foo()
{
Console.WriteLine("Foo from Base");
}
}
class Derived : Base
{
internal new void Foo()
{
Console.WriteLine("Foo from Derived");
}
}
static void Main()
{
Base b = new Derived();
b.Foo();
}
Then he will execute the method Base Foo, not Derived. In other words, it will print Foo from Base. In the first case, he would still execute the method Derivedand print Foo from Derived.
source
share