0
0
C Sharp (C#)programming~5 mins

Sealed classes and methods in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Sealed classes and methods stop other classes from changing or extending them. This helps keep your code safe and clear.

When you want to stop a class from being inherited by others.
When you want to prevent a method from being changed in child classes.
When you want to make sure your code works exactly as you wrote it.
When you want to improve performance by avoiding extra method lookups.
When you want to protect important parts of your code from accidental changes.
Syntax
C Sharp (C#)
sealed class ClassName
{
    // class members
}

// class DerivedClass : ClassName // This will cause an error
// {
// }

class BaseClass
{
    public virtual void MethodName() { }
}

class DerivedClass : BaseClass
{
    public sealed override void MethodName() { }
}

class MoreDerivedClass : DerivedClass
{
    // Cannot override MethodName here
}

A sealed class cannot be inherited by any other class.

A sealed method cannot be overridden by any further derived classes.

Examples
This shows a sealed class Animal. No class can inherit from it.
C Sharp (C#)
sealed class Animal
{
    public void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

// Trying to inherit Animal will cause an error
// class Dog : Animal { }
This shows a sealed method Start in class Car. It cannot be changed in SportsCar.
C Sharp (C#)
class Vehicle
{
    public virtual void Start() {
        Console.WriteLine("Vehicle starts");
    }
}

class Car : Vehicle
{
    public sealed override void Start() {
        Console.WriteLine("Car starts");
    }
}

class SportsCar : Car
{
    // Cannot override Start here because it is sealed in Car
}
Sample Program

This program shows a sealed class Fruit which cannot be inherited. It also shows a sealed method Grow in class Tree that cannot be overridden by Oak.

C Sharp (C#)
using System;

sealed class Fruit
{
    public void ShowType() {
        Console.WriteLine("This is a fruit.");
    }
}

// Uncommenting the following line will cause a compile error
// class Apple : Fruit { }

class Plant
{
    public virtual void Grow() {
        Console.WriteLine("Plant grows");
    }
}

class Tree : Plant
{
    public sealed override void Grow() {
        Console.WriteLine("Tree grows tall");
    }
}

class Oak : Tree
{
    // Cannot override Grow() here because it is sealed in Tree
}

class Program
{
    static void Main() {
        Fruit fruit = new Fruit();
        fruit.ShowType();

        Plant plant = new Plant();
        plant.Grow();

        Tree tree = new Tree();
        tree.Grow();

        Oak oak = new Oak();
        oak.Grow();
    }
}
OutputSuccess
Important Notes

Sealed classes and methods help keep your code safe from unwanted changes.

Trying to inherit a sealed class or override a sealed method will cause a compile error.

Sealing methods can also improve performance slightly by avoiding virtual method calls.

Summary

Sealed classes cannot be inherited.

Sealed methods cannot be overridden further.

Use sealing to protect your code and improve clarity.