How to use abstract and sealed class in C# application
Understand abstract and sealed class in C#
In this tutorial we will understand the concept of sealed
and abstract class in C#. Both are modifier and we can modify any C# class
using them. We will understand them with suitable example.
Understand abstract class
Sealed class is used to create a concrete class and when we do
not want to create object of this class. Then question may come in mind, where
we can use sealed class? Think about template design pattern, where there is
need to create one concrete class and we have to inherit it in another class. So,
when there is need to create one concrete class and inherit them in another
class, we can define this class as abstract class and the point to remember “We
cannot create instance of abstract class”. Try to understand below example.
In this example we have define abstract class and trying to
create object of this class. Compiler will not allow to create object of
abstract class.

We can inherit class from abstract class.
As we have discussed the main purpose of abstract class is implement
template design pattern. Try to understand below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp
{
public abstract class Person
{
public string name { get; set; }
public string surname { get; set; }
}
public class Student:Person
{
public string CollegeName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.name = "Sourav ";
s.surname = "Kayal";
s.CollegeName = "Anna University";
Console.WriteLine("Name:- " + s.name);
Console.WriteLine("Surname:- " + s.surname);
Console.WriteLine("College Name:- " + s.CollegeName);
Console.ReadLine();
}
}
}
Here is sample output.

Understand sealed class in C#
Sealed class is used to prevent re-use of this class. There
might be some situation where we define a class and want to ensure that no
class will be able to use the property of this class. So, the main idea behind sealed
class is to prevent re-use. Try to understand below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp
{
public sealed class Person
{
public string name { get; set; }
public string surname { get; set; }
}
public class Student:Person
{
public string CollegeName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
}
}
}
Here is output and we are seeing that compiler is not
allowing inheriting any class from sealed class.

Conclusion:-
In this article we have learned how to use sealed class and abstract class in C#. Hope you have understood the concept and enjoyed it.