Introduction
For that purpose we have keyword called “Sealed” in OOPS. When we defined class with keyword “Sealed” then we don’t have a chance to derive that particular class and we don’t have permission to inherit the properties from that particular class.
Here we can declare a class that is derived from another class can also be sealed. Check below example
From above example we can easily say that sealed classes can’t inherit or acquire properties from parent class.
Here I will explain what are the sealed classes in c# and uses of sealed classes in c#.
Description:
In previous article I explained clearly about OOPS concepts now I will explain what is the use of sealed classes in c#. Generally if we create classes we can inherit the properties of that created class in any class without having any restrictions. In some situation we will get requirement like we don’t want to give permission for the users to derive the classes from it or don’t allow users to inherit the properties from particular class in that situations what we can do?
For that purpose we have keyword called “Sealed” in OOPS. When we defined class with keyword “Sealed” then we don’t have a chance to derive that particular class and we don’t have permission to inherit the properties from that particular class.
Example to declare class as sealed
sealed class Test
{
public int Number;
public string Name;
}
|
If the class declared with an access modifier, the Sealed keyword can appear after or before the public keyword.
Example
public sealed class Test
{
public int Number;
public string Name;
}
|
Here we can declare a class that is derived from another class can also be sealed. Check below example
public sealed class Test
{
public int Number;
public string Name;
}
|
public sealed class child1:Test
{
public string Name;
}
|
Example to use Sealed Keyword First create console application in C# and write the following code in Program.cs file and test it once.
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Irregular tri = new Irregular(42.73, 35.05);
tri.Describe("Irregular");
}
}
public abstract class Triangle
{
private double bs;
private double hgt;
public Triangle(double length , double height)
{
bs = length;
hgt = height;
}
public virtual double Area()
{
return bs * hgt / 2;
}
public void Describe(string type)
{
Console.WriteLine("Triangle - {0}", type);
Console.WriteLine("Base: {0}", bs);
Console.WriteLine("Height: {0}", hgt);
Console.WriteLine("Area: {0}", Area());
}
}
sealed public class Irregular : Triangle
{
public Irregular(double Base, double Height): base(Base, Height)
{
}
}
}
|
Output of above sample will be like this
|
From above example we can easily say that sealed classes can’t inherit or acquire properties from parent class.
Another Important information is if we create a static class, it becomes automatically sealed. This means that you cannot derive a class from a static class. So, the sealed and the static class have in common that both are sealed. The difference is that you can declared a variable of a sealed class to access its members but you use the name of a static class to access its members.
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
18 comments :
Nice .. Article
Public sealed class Test
{
public int Number;
public string Name;
}
Public sealed class child1:Test
{
public string Name;
}
Will the above code work ? since test class is already sealed class.
????????
above code does not work, as class Test is already marked as sealed
I like this article... But what is the use of access specifier using before sealed class
its not work
Nice article keep it up.
Suresh one dout what is the difference between web application and windows application plz tell me.......
Gud Artile..Keep writing such articles...
I have 1 doubt...Can u Explain me Whats a different B?W Private and Sealed?
not so clear, may be this is the first article from suresh which i havent understood. need more basic and elaborative for starters.
Private is an Access Modifier with a least accessibility .Sealed is used to prevent other class from inherit Sealed and it also uses on method/property to prevent from overriding specific virtual methods /properties.
how to remove the blank space in crystal report
nice article
fdf
Sealed class cannot be inherited by another sealed class
above code you mention that is a public sealed called can inherit properties of base class but class also should be public sealed class.
Public sealed class Test
{
public int Number;
public string Name;
}
Public sealed class child1:Test
{
public string Name;
}
Will the above code work ? since test class is already sealed class.
????????
according to me this code is correct.like above you mantion,
no sealed class can not be inherited
The given code is not working. Due to 2-sealed classes are there and we can't inheritance right? Then how it works?
Note: Only a member of this blog may post a comment.