By 22/07/2015 11 Comments

Sealed Keyword In C#

  • The keyword “sealed” prevents from inheritance creation in C#. It prevents the base classes to inherit to derived classes.
  • The “sealed” keyword cannot be used with abstract classes and methods.
  • The “sealed” keyword can also be used with virtual methods which is overridden in derived classes and it prevents to virtual methods to be further overrides within next derived classes.

Example – 1, sealed method:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        protected double AspMarks;
        protected double CSharpMarks;

        // Parametrized constructor which sets marks for each subject.
        public MarkSheet(double aspMarks, double cSharpMarks)
        {
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;
        }

        // A Double return type method is declared with virtual keyword.
        public virtual double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks;
        }

        // Double return type method declaration which returns obtain marks of subjects.
        public double GetPercentage(double obtainMarks, double totalMarks)
        {
            return (obtainMarks / totalMarks) * 100.0;
        }
    }

    // BcsMarkSheet class inherits MarkSheet class
    class BcsMarkSheet : MarkSheet
    {
        double PhpMarks;

        // Parametrized constructor which sets marks for each subject in derived class.
        public BcsMarkSheet(double aspMarks, double cSharpMarks, int phpMarks)
            : base(aspMarks, cSharpMarks)
        {
            this.PhpMarks = phpMarks;
        }

        // Double return type methods which returns each subject marks,
        public double GetAspMarks()
        {
            return this.AspMarks;
        }

        public double GetCsharpMarks()
        {
            return this.CSharpMarks;
        }

        public double GetPhpMarks()
        {
            return this.PhpMarks;
        }

        // Notice the following method, this is a “virtual” and “sealed” method.
        sealed public override double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Derived class object creation
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(85, 70, 60);

            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1: Asp.net");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetAspMarks());
            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetCsharpMarks());
            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetPhpMarks());
            Console.WriteLine("Obtain marks: {0}", bcsMarkSheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", bcsMarkSheet.GetPercentage(bcsMarkSheet.GetObtainMarks(), 300));
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Bcs Mark sheet information:
        Subject 1: Asp.net
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Obtain marks: 215
        Percentage: 71.6666666666667
        
       */
    }
}


Example – 2, sealed class:

using System;
namespace csharpBasic
{
    // A base class declaration with sealed keyword.
    sealed class MarkSheet
    {
        public double AspMarks;
        public double CSharpMarks;

        public MarkSheet(double aspMarks, double cSharpMarks)
        {
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;
        }

        public virtual double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks;
        }

        public double GetPercentage(double obtainMarks, double totalMarks)
        {
            return (obtainMarks / totalMarks) * 100.0;
        }
    }

    /* BcsMarkSheet class is trying to inheriting sealed MarkSheet class.
       This program will not be compiled and generate compile-time error
       because a sealed base class does not inherit. */
    class BcsMarkSheet : MarkSheet
    {

    }
}






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .