Demonstrates the use of virtual, override, sealed and new keywords on methods.
| public class Base | |
| { | |
| public virtual void Method() | |
| { | |
| Console.WriteLine("Base (can be overridden.)"); | |
| } | |
| public void CantOverride() | |
| { | |
| Console.WriteLine("Can't be overridden (but can be newed)."); | |
| } | |
| } | |
| public class Overrider : Base | |
| { | |
| public override void Method() | |
| { | |
| Console.WriteLine("Override (can be overridden.)"); | |
| } | |
| public new void CantOverride() | |
| { | |
| Console.WriteLine("Can't be overridden (but can be newed)."); | |
| } | |
| } | |
| public class SealedOverrider : Overrider | |
| { | |
| public sealed override void Method() | |
| { | |
| Console.WriteLine("Sealed override (can't be overridden.)"); | |
| } | |
| } | |
| public class Newer : SealedOverrider | |
| { | |
| public new void Method() | |
| { | |
| Console.WriteLine("New"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment