Override: When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.
New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.
New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.
public class BaseClass { public virtual void AMethod() { } } public class DerivedClass : BaseClass { public override void AMethod() { } } // ... BaseClass baseClass = new DerivedClass(); baseClass.AMethod();
public class BaseClass { public virtual void AMethod() { } } public class DerivedClass : BaseClass { public new void AMethod() { } } // ... BaseClass baseClass = new DerivedClass(); baseClass.AMethod(); DerivedClass derivedClass = new DerivedClass(); derivedClass.AMethod();
Comments
Post a Comment