Sealed classes---staticclasses

Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited. 

In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET the NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error. 



using System;

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

namespace sealedclasses
{
   sealed  class A
    {
        public void show()
        {
            Console.WriteLine("Show");
        }
    }
   /* class B : A  // Error because ClassA Sealed
    {
        public void Display()
        {
            Console.WriteLine("display");
        }
    }*/
    class Program
    {
        static void Main(string[] args)
        {
            A obja = new A();
            obja.show();
            Console.ReadKey();
        }
    }
}


A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new  keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself. For example, if you have a static class that is named UtilityClass that has a public method named MethodA, you call the method as shown in the following


 example:.......

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

namespace staticclasses
{
    class Account
    {
        public int acno;
        public string acname;
        public static int minbal;
    }
    class Test
    {
        public void Show()
        {
            Console.Write("Non Static Method");
        }
        public static void Display()
        {
            Console.Write("Static Method");
        }
    }
    class Program
    {
        int x;
        static int y;
        static void Main(string[] args)
        {
            Account objnlrbranch = new Account();
            objnlrbranch.acno = 100;
            objnlrbranch.acname = "balaji";
            Account.minbal = 5000;
            Account objgdrbranch = new Account();
            objgdrbranch.acno = 101;
            objgdrbranch.acname = "krishna";
            Console.WriteLine(Account.minbal);

            /*Program obj = new Program();
            obj.x = 10;
            // x = 10; Error because x is non static
            y = 20; //because y is static*/

            Test obj = new Test();
            obj.Show();
            Test.Display();
        }
    }
}

Comments