Monday, July 21, 2008

Sealed Class

A Sealed is a modifer in .NET.The sealed modifier is equivalent to marking a class with the final keyword in Java.We can use this for the following reasons.

1. when youn want to provide security for the class.you declare sealed class.If you declare sealed class the class can not inerited to other class.
2. The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.


using System;
sealed class SealedClass{
public int x;
public int y;
}

class MainClass{
static void Main(){
SealedClass sc = new SealedClass();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
}
}


Output :
x = 110, y = 150