I have a base abstract class that implements Method1
abstract class Base
{
public void Method1()
{
Console.WriteLine("Method1 Base");
}
public abstract void Method2();
}
and a base2 abstract class that inherits from base and hide Method1
abstract class Base2 : Base
{
public new abstract void Method1();
}
and a concrete class that inherits from base2 and implements Method1
class Concrete : Base2
{
public override void Method1()
{
Console.WriteLine("Method1 Concrete");
}
public override void Method2()
{
Console.WriteLine("Method2 Concrete");
}
}
Now I have 2 cases in the programme and I was expecting the same result, that the Method1 form the concrete class get called, but in the first case the Method1 form the base class get called. I dont understand why because I am instantiating objects with new Concrete()
class Program
{
static void Main(string[] args)
{
Base obj1 = new Concrete();
obj1.Method1(); //output: "Method1 Base" why????
obj1.Method2(); //output: "Method2 Concrete"
Concrete obj2 = new Concrete();
obj2.Method1(); //output: "Method1 Concrete"
obj2.Method2(); //output: "Method2 Concrete"
}
}