Click to See Complete Forum and Search --> : new Keyword
vikas_arora80
February 6th, 2001, 11:09 AM
The C# language asks foe a "new" keyword when I derive an interface "IDerived" from interface "IBase" and the function "getName()" is declared in both Interfaces. Does this mean , that C# doesnot allow method overridding.
Java Developer
SRG Systems Inc.
CT, USA
Ilya Kovalenko
February 8th, 2001, 11:58 PM
C# supports virtual functions overriding. But by default your IDerive.getName() isn't an override of your IBase.getName(). It's a NEW virtual function although it has the same name. The compiler warns you. If you specify explicitly that IDerive.getName()is a new virtual function by using the new keyword it will stop warn you. But if you want IDerive.getName()to be an override of IBase.getName()you MUST use the override keyword.
public interface IBase
{
public virtual getName();
}
public interface IDerive: IBase
{
public override virtual getName();
}
Tom Archer
February 13th, 2001, 04:48 AM
Actually, the distinction is as follows.
If you want polymorphism, then you need to use the virtual/override keyword pair as you suggested.
However, if you simply want to supply a new (overridden) version of the method, then you need only define the method as new.
Cheers,
Tom Archer - CodeGuru
Inside C# (early 2001)
Teach Yourself Visual InterDev in 24 Hours
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.