Csharp/CSharp Tutorial/Class/sealed
internal sealed class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public abstract class MyBase
{
}
internal class MyClass : MyBase
{
}
public interface IMyBaseInterface
{
}
internal interface IMyBaseInterface2
{
}
internal interface IMyInterface : IMyBaseInterface, IMyBaseInterface2
{
}
internal sealed class MyComplexClass : MyClass, IMyInterface
{
}
class Program
{
static void Main(string[] args)
{
MyComplexClass myObj = new MyComplexClass();
Console.WriteLine(myObj.ToString());
}
}
Sealed Classes
sealed class MyClass
{
MyClass() {}
}
Sealed methods
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
sealed types and methods.
To prevent a class from being inherited, precede its declaration with sealed.
sealed class FooSealed {}
class BaseClass
{
protected virtual void Bar() { /*...*/ }
}
class Derived : BaseClass
{
protected override sealed void Bar() { /* ... */ }
}