I have taken a windows application and a button control in it. I want to declare a variable as a sealed and use that variable with in same class as we cannot use in other class.
class test { public sealed int x = 5; public void abc() {
MessageBox.Show(x.ToString());
} } class best :test { // public sealed int y = 10;// error..the modifer sealed is Not valid for this item //private sealed int y = 10; // "" //protected sealed int y = 10; // "" public void print() { //MessageBox.Show(y.ToString()); // even when Iam commenting this line I am getting the same above error //MessageBox.Show(x.ToString()); }
} private void button1_Click(object sender, EventArgs e) { test t = new test(); t.abc(); best b = new best(); b.print(); }
Even Iam commenting the derived class also Iam getting the same error. Error The modifier 'sealed' is not valid for this item
You can use sealing to limit the ways in which others can extend the framework. When you seal a class other classes cannot inherit from it. When you seal a member derived classes cannot override the implementation of the member.
You should not seal types and members by default. Sealing prevents customization of library types and members and impacts the perception of usability for other developers.
In addition extensibility is one of the fundamental benefits of using an OO framework. So you should carefully weigh decisions that restrict this benefit.
Variables can not be declred as sealed this is valid only for class that means if a class declared as a sealed class we can't inherit that class. Thanks.
sealed word is always use with class .and when u make any class sealed thn it is impossible to access that class member. second thing u could not make any method sealed in sealed class. bt u can declare method sealed after overidding it.by this u make that method further in accessible.