Virtual: Use this keyword to make it overridden in a derived class.
Abstract: An abstraction method that is overridden by a subclass, or continues to exist for an abstract method and is implemented by its child subclasses.
Override: Overrides an abstract implementation or virtual method of a parent class method, property, or event.
NEW: Explicitly hides the inherited members from the parent class.
Background code:
Public abstract class animal{public abstract void Eat (); Public virtual void sleep () { HttpContext.Current.Response.Write ("Animal is sleeping!
| Foreground call |
Effect |
protected void Page_Load (object sender, EventArgs e) { Animal an1 = new Horse (); An1. Eat (); An1. Sleep (); Animal an2 = new Cat (); An2. Eat (); An2. Sleep (); Horse an3 = new Horse (); An3. Eat (); An3. Sleep (); Cat An4 = new Cat (); An4. Eat (); An4. Sleep (); } |
|
Add:
When sealed modifies a method, the sealed must be used with override.
Sealed will enable you to allow classes to inherit from your class and prevent them from overriding specific virtual methods or virtual properties
public class cat:animal{public sealed override void Eat () { HttpContext.Current.Response.Write ("Cat is eating!") <br/> "); } Public new void Sleep () { HttpContext.Current.Response.Write ("Cat is lying on sleeping!
At this point, in the Litcat class, the override eat method is not present.
C#--virtual,abstract,override,new,sealed