E
E
Evgeny Ivanov2020-12-22 11:58:18
C++ / C#
Evgeny Ivanov, 2020-12-22 11:58:18

How to use (what is the meaning of) sealed?

Here is the documentation
https://docs.microsoft.com/en-us/dotnet/csharp/lan...

Here is an example from the documentation

class A {}
sealed class B : A {}

That is, inheritance from class B is not possible. It's clear.

And already here the question arises, why not write like this
sealed class A {}
//class B : A {} //Error

That is, in this example, I specifically showed that inheritance is not possible.
In the first example, this is not shown so clearly. There, the sealed modifier was applied to the inherited class B and it was said that inheritance from class B is not possible already. That is, some third class cannot be inherited.

And then there is another example. (Here it is the third class Z.)
class X
{
    protected virtual void F() { Console.WriteLine("X.F"); }
    protected virtual void F2() { Console.WriteLine("X.F2"); }
}

class Y : X
{
    sealed protected override void F() { Console.WriteLine("Y.F"); }
    protected override void F2() { Console.WriteLine("Y.F2"); }
}

class Z : Y
{
    // Attempting to override F causes compiler error CS0239.
    // protected override void F() { Console.WriteLine("Z.F"); }

    // Overriding F2 is allowed.
    protected override void F2() { Console.WriteLine("Z.F2"); }
}

Please note that here sealed goes in conjunction with override.

As I understand it, sealed is sealed. That is, what is sealed is not inherited, does not change, etc.
They put sealed - everything is "armor". No one will change, inherit (with some exceptions and tricks).

But this code is not correct
class classA
 {
     protected string field_1 = null;
     public sealed string Field_1 { get { return field_1; } set {field_1 = value;} }
 }

 class classB : classA
 {
    public   string Field_1 { get { return field_1; } set { if (value != "1") { field_1 = value; } } }

 }

The Field_1 property cannot be sealed. does not contain an override modifier.
Why? I don't want my property to be overridden or overridden.

Here is my other example
class classA
 {
     protected string field_1 = null;

     public sealed virtual string Field_1 { get { return field_1; } set {field_1 = value;} } //sealed нужно убрать отсюда
 }


 class classB : classA
 {
    public override string Field_1 { get { return field_1; } set { if (value != "1") { field_1 = value; } } } //sealed должно быть тут

 }

Likewise. Says - put sealed to override. That is, I can prevent the redefinition of the classB property.
What is the problem with classA? I want no one to override its property. I put sealed.
And the standard error is impossible ... because. I don't see it sealed.

Why is override required for sealing? Why can I only apply property sealing to the second class?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Bannikov, 2020-12-22
@logpol32

Why is override required for sealing?

Because if the method is not virtual (if it does not have the virtual, override or abstract modifier), then it cannot be overridden anyway.
By default, all methods are non-virtual.

V
Victor Bomberow, 2020-12-22
@majstar_Zubr

Sealed translates as "sealed, sealed". What exactly is being sealed?
1) Application to class declaration. The first example shows the context in which the keyword is used. The inheritance functionality is sealed at the first level of inheritance. Your counterexample does not provide new information to the compiler, because at compile time the compiler can decide for itself whether a pointer to a virtual function table should be used when storing class objects. Therefore, there the word sealed is syntactically correct, but lexically useless.
2) Application to the declaration of a class member. Sealing implies having something that can be sealed. Namely, the inheritance function. If you need to seal the functionality of inheritance at some level, you do it. If you are trying to print something on

zero
the base class level, then this is, in C# terminology, an unexpressed thought. You shouldn't use inheritance at all in such a case (don't declare virtual). The keyword final (C++) could express such an idea, but in C # it is not, in it OOP is used in a slightly different context (where, roughly speaking, types can be manged and unmanaged, and the programmer does not have full control over sizeof type objects).
That is why the behavior is implemented as implemented and
When applied to a method or property, the sealed modifier must always be used with override.

Why can I only apply property sealing to the second class?

In general, the inheritance functionality is calculated by inheritance levels, and there it is just the first, not the second. The second class before our eyes is the first level of inheritance and it is sealed.

F
freeExec, 2020-12-22
@freeExec

Instead sealed, I can use newand calmly write my own logic without delving into what the parent had there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question