Sealed Class : -


  • A class from which it is not possible to Create / Derive a new class is known as Sealed Class
  • To make any class as sealed class we use sealed keyword
  • A sealed class is completely opposite to an abstract class
  • A sealed class can’t contain abstract functions
  • A sealed class cannot contain virtual functions
  • A sealed class should be the bottom most class with in the inheritance hierarchy
  • A sealed class never be used as base class
  • A sealed class especially used to avoid further inheritance
  • The sealed keyword can be used with classes, instance methods and properties

Example :-

Class Diagram :-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CASealedExample
{
    class ClsEmployee
    {
        protected int EmpId, EAge;
        protected string EName, EAddress;
        public virtual void GetEmpData()
        {
            Console.WriteLine("Enter Employee details");
            Console.WriteLine("Enter Employee id ");
            this.EmpId = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Employee Name");
            this.EName = Console.ReadLine();
            Console.WriteLine("Enter Employee Address");
            this.EAddress = Console.ReadLine();
            Console.WriteLine("Enter Employee Age");
            this.EAge = Convert.ToInt32(Console.ReadLine());
        }
        public virtual void DisplayEmpData()
        {
            Console.WriteLine("Employee id is     " + this.EmpId);
            Console.WriteLine("Employee Name is   " + this.EName);
            Console.WriteLine("Employee Address is" + this.EAddress);
            Console.WriteLine("Employee Age is    " + this.EAge);
        }
    }
    sealed class ClsManager : ClsEmployee
    {
        double Bonus, CA;
        public override void GetEmpData()
        {
            Console.WriteLine("Enter Manager Details");
            Console.WriteLine("Enter Manager Id is ");
            EmpId = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Manager Name is ");
            EName = Console.ReadLine();
            Console.WriteLine("Enter Manager Bonus is");
            Bonus = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter Manager CA is ");
            CA = Convert.ToDouble(Console.ReadLine());
        }
        public override void DisplayEmpData()
        {
            Console.WriteLine("Manager id is   " + EmpId);
            Console.WriteLine("Manager Name is " + EName);
            Console.WriteLine("Manager Bonus is" + Bonus);
            Console.WriteLine("Manager CA is   " + CA);
        }
    }
    class ClsSealed
    {
        static void Main(string[] args)
        {
            ClsManager Obj1 = new ClsManager();
            Obj1.GetEmpData();
            Obj1.DisplayEmpData();
            Console.Read();
        }
    }
}




Output :-






Difference between An Abstract class and a Sealed class

SNo
Abstract class
Sealed class
1
A class which contains one or more abstract functions is known as an abstract class
A class from which it is not possible to derive a new class is known as sealed class
2
Abstract class can contain Non-Abstract and abstract functions
Sealed class can contain Non-Abstract functions, can not contain abstract / virtual functions
3
Creating a new class from an abstract class is compulsory to consume it
It is not possible to create a new class from sealed class
4
An abstract class cannot be instantiated directly, we need to create object for derived class to consume an abstract class
We should create an object for sealed class only to consume it
5
Use abstract keyword to make any class as abstract
Use sealed keyword to make any class as sealed
6
An abstract class cannot be the bottom most class with in the inheritance hierarchy
Sealed class should be the bottom most class with in the inheritance hierarchy
7
An abstract class should be used as base class only
A sealed class cannot be used as base class



No comments:

Post a Comment