Sealed methods : sealed « Class « C# / CSharp Tutorial






using System;

public class BaseClass
{
  public string lastName;
  public string firstName;

  public BaseClass(string lastName, string firstName)
  {
    this.lastName = lastName;
    this.firstName = firstName;
  }

  public virtual void Accelerate()
  {
    Console.WriteLine("In BaseClass Accelerate() method");
    Console.WriteLine(firstName + " accelerating");
  }

}

public class DerivedClass : BaseClass
{
  public DerivedClass(string lastName, string firstName) : base(lastName, firstName)
  {
  }

  sealed public override void Accelerate()
  {
    Console.WriteLine("In DerivedClass Accelerate() method");
    Console.WriteLine(firstName + " accelerating");
  }

}

class MainClass
{

  public static void Main()
  {
    DerivedClass myDerivedClass = new DerivedClass("A", "M");

    Console.WriteLine("Calling myDerivedClass.Accelerate()");
    myDerivedClass.Accelerate();
  }
}
Calling myDerivedClass.Accelerate()
In DerivedClass Accelerate() method
M accelerating








7.37.sealed
7.37.1.Sealed methods
7.37.2.sealed types and methods.
7.37.3.Sealed Classes
7.37.4.internal sealed class