In this post we will discuss about Sealed class and Sealed method in C#.Net.
Also check out:
- Caching in Asp.Net
- Constraints in SQL Server 2008
- How to make fade out animation effect in asp.net using Ajax AnimationExtender Control?
Sealed class:
C#.Net allows classes and methods to be declared as sealed. In the case of a class, this means you can’t inherit from that class. If you want no one should extend your class then you can make that class as Sealed class.
To make a class as Sealed we have to use the Sealed keyword.
sealed class MySealedClass
{
// Code will go here
}
Now suppose you want to inherit this class in any other class like below, then it will give a compilation error.
class MyDerivedClass: MySealedClass
{
// Code will go here
}
Sealed Method:
Similarly C#.Net allows us to make methods as Sealed so that no other class can use the method.
sealed class MySealedClass
{
public sealed override void MyMethod()
{
//Code will go here
}
}
But if you try to access the sealed method in the child class then it will give a compailation error.
class MyDerivedClass: MySealedClass
{
public override void MyMethod()
{
//Code will go here
}
}
In order to use the sealed keyword on a method or property, it must have first been overridden from a base class.
Also check out:
- Caching in Asp.Net
- Constraints in SQL Server 2008
- How to make fade out animation effect in asp.net using Ajax AnimationExtender Control?
Sealed class:
C#.Net allows classes and methods to be declared as sealed. In the case of a class, this means you can’t inherit from that class. If you want no one should extend your class then you can make that class as Sealed class.
To make a class as Sealed we have to use the Sealed keyword.
sealed class MySealedClass
{
// Code will go here
}
Now suppose you want to inherit this class in any other class like below, then it will give a compilation error.
class MyDerivedClass: MySealedClass
{
// Code will go here
}
Sealed Method:
Similarly C#.Net allows us to make methods as Sealed so that no other class can use the method.
sealed class MySealedClass
{
public sealed override void MyMethod()
{
//Code will go here
}
}
But if you try to access the sealed method in the child class then it will give a compailation error.
class MyDerivedClass: MySealedClass
{
public override void MyMethod()
{
//Code will go here
}
}
In order to use the sealed keyword on a method or property, it must have first been overridden from a base class.