The reason is that Java has default methods virtual. In C #, virtual methods should be explicitly marked as such.
The following C # code is equivalent to Java code - note the use virtualin the base class and overridein the derived class:
class Base
{
public virtual void foo()
{
System.Console.WriteLine("base");
}
}
class Derived
: Base
{
static void Main(string[] args)
{
Base b = new Base();
b.foo();
b = new Derived();
b.foo();
}
public override void foo()
{
System.Console.WriteLine("derived");
}
}
The C # code you posted hides the method fooin the class Derived. This is something you usually don't want to do, because it will cause inheritance issues.
, , , :
Base b = new Derived();
b.foo(); // writes "base"
((Derived)b).foo(); // writes "derived"
, , "" .