OOPs

What is a Sealed Class and Sealed Members in C#?

What is a Sealed Class and Sealed Members in C#?

Sealed Class and Sealed Members



Sealed classes are the reverse of abstract classes
It is not possible to utilise a sealed class as a base class. It cannot therefore also be an abstract class.



When to use Sealed Class
This, is used to stop a developer to extend a class or override a method accidentally.
Use Sealed modifier is used to declare a class as sealed
One of the best uses of sealed classes is when you have a class with static members.
When you want to prevent other developers from creating a derived class (or restrict the class to be inherited) and modifying the behavior of the class in unintended ways.
If you are designing a class for a specific use case and you don't expect it to be used as a base class for other classes, you can use the sealed keyword to prevent other developers from creating a derived class that could break the intended behavior of the class.



Characteristics of a Sealed Modifler

1. you can apply the sealed modifier to a property or method that overrides a virtual property or method
2. The sealed properties and methods seal their implementations to prevent overriding
3. Abstract methods cannot be used in a sealed class.
4. In the inheritance hierarchy, it must be the bottom-most class.
5. Sealed class purposely used to avoid inheritance.
6. The sealed keyword used with methods, classes, properties, and instance.

Example #1 Sealed Class

public class User
{
    protected int id, age;
    protected string name, address;
    public virtual void GetDetails(){
        Console.WriteLine("Enter User Details");
        id = 1000;
    }
    public virtual void DisplayDetails()
    {
        Console.WriteLine("User Id : " + id);
    }
}


//Sealed class
public sealed class Employee : User
{
    double Bonus, CA;
    public override void GetDetails(){
        Console.WriteLine("Enter Manager Details");
        id = 2000;
    }
    public override void DisplayDetails()
    {
        Console.WriteLine("Manager Id : " + id);
    }

    // Sealed Method
    public sealed void Display() { }
    public void EmployeeAge(employeeId){
        return 45;
    }
}

//If you attempt to subclass the Employee, you’ll get an error.  
//'FullTimeEmployee': cannot derive from sealed type 'Employee'
class FullTimeEmployee : Employee
{
}


class Program
{  
    public static void Main (string[] args)
    {
        Employee emp = new Employee();
        //Now if we try to call "EmployeeAge" method through the object "emp" we could find "EmployeeAge" method

        SealedDemo _sealedObject=new SealedDemo();
        int age= emp.EmployeeAge(1);
        Console.WriteLine("Result="+age.ToString());

        emp.GetDetails();
        emp.DisplayDetails();
    }

    // Output:
    // Result=45;
    // Enter Manager Details
    // Manager Id : 2000
}




Sealed Methods
When a method is considered sealed, it means that it belongs to a parent class and cannot be overridden by a child class.

Example #1 Sealed Members

public class Flat
{
    public virtual void display()
    {
        Console.WriteLine("Not ready yet...");
    }
    public virtual void features()
    {
        Console.WriteLine("Not ready yet...");
        balcony();
    }
    public virtual void balcony()
    {
        Console.WriteLine("Not ready yet...");
    }
}

public class OneBHK : Flat
{
    public override void display()
    {
        Console.WriteLine("This is 1 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }
    public override void balcony()
    {
        Console.WriteLine("\tBalcony: Rectangular\n");
    }

}


public class TwoBHK : OneBHK
{
    public override void display()
    {
        Console.WriteLine("This is 2 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }
    public sealed override void balcony()
    {
        Console.WriteLine("\tBalcony: Circular\n");
    }
}

//Lets Mark ThreeBHK class as sealed as We don't allowed to create 4BHK or above.
//We cannot extend ThreeBHK class as marked as sealed.
public sealed class ThreeBHK : TwoBHK
{
    public override void display()
    {
        Console.WriteLine("This is 3 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }

    // We cannot override and implement its own balcony type for ThreeBHK
    // As we have restriction that all flats above TwoBHK will have circular type and cannot desinged their own type.
}




Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram