What is sealed in C #?

11

I saw a class in C # that was declared like this, in an answer I read in SOen:

public sealed class Link
{
     // Resto do código
}

What is the keyword sealed in the above case?

    
asked by anonymous 14.06.2018 / 19:54

5 answers

12

Well, in summary the sealed is to indicate that the class can not be inherited by others. It is the same as final of Java.

Many say it should be the default, since inheritance is almost never the solution. Inheritance brings a very strong baggage and a burden for maintenance since any change in this class will potentially affect the functioning of all derivatives.

A data structure as a class is useful, but not so much inheritance. Of course, it has its uses. Organizing everything together in the object is much more advantageous and helps a lot more than allowing direct reuse. In most cases the person wants to make an inheritance should make a composition.

A struct is implicitly sealed , and should have standardized this on the class. You should have an optional word like open , inheritable , or even virtual to use something that already exists, or something similar to that, what matters is the idea. The fundamental difference of a class is that it is always by reference, and unless optimization has the storage location in heap (some today admit that it could be better even if this is not standard and the programmer has to say where it goes, but it's a small chain for C #)

Sealed classes are simple and reliable, require less testing and can evolve better. Of course, they were well made. Open classes for inheritance are problematic even when everything is done right. It gets worse when the class can be inherited and instantiated. There are those who consider that classes open to inheritance should always be abstract, which I agree with more and more. Many OOP problems would not exist like this. But I also admit that it has case that it may be helpful to have both, done very carefully.

Then use sealed by default until you're sure you need inheritance. Then draw the class for the inheritance, which is absurdly more difficult.

In C # the idea of the default inheritance was copied from Java and was a bad idea. It already existed before, but it made more sense in other languages. Java, for its philosophy, missed and C # was on the rise.

In methods

This attribute is also used in methods, much more rarely. In this case you are forbidding a method to be overlapped when originally it was virtual, and carrying polymorphic. It only makes sense to use it in methods that are inherited virtually and that you want to prevent it from remaining virtual from there. So it only makes sense to exist in an already inherited class and in a method that in ascending has been marked as virtual . Methods not explicitly virtual can be considered sealed implicitly.

Again I consider it a mistake. If you want to remain virtual, you should have to say that. Although in this case there are its disadvantages. I prefer the implicit one for coherence, but being explicit has its merit.

The name is weird, but it was what it had. It would be nonvirtual .

    
14.06.2018 / 20:43
5

It is a modifier that, in classes, defines that other classes can not inherit from the specified one.

public class A { }
public sealed class B : A { } // Funciona normal. B herda de A e não pode ser herdada.
public class C : B { }        // Erro de compilação.

This modifier can also be used on members of classes that can be inherited. This causes the child class to use the behavior defined in the parent class but can not change it.

class A
{
    protected virtual void Metodo1() => Console.WriteLine("Classe A -> 1");
    protected virtual void Metodo2() => Console.WriteLine("Classe A -> 2");
}

class B : A
{
    protected sealed override void Metodo1() => Console.WriteLine("Classe B -> 1");
    protected override void Metodo2() => Console.WriteLine("Classe B -> 2");
}

class C : B
{         
    protected override void Metodo1() => Console.WriteLine("Classe C -> 1");     
    // (^) Erro de compilação.
    protected override void Metodo2() => Console.WriteLine("Classe C -> 2");
}
    
14.06.2018 / 20:04
4

In the case of classes, the modifier sealed is to indicate that it can not be inherited.

class A {}      
sealed class B : A {}

That is, B can inherit from A, but if I create a C class and try to inherit from B, it will cause a compilation error.

// Causa erro
class C : B {}

The same thing happens with methods overriding. If you try to overwrite a method with the sealed modifier, it will also cause a compilation error.

In your example, the modifier indicates that no other class can inherit from class Link

    
14.06.2018 / 20:01
4

sealed , when used in a class declaration, prevents other classes from inheriting from it.

public class TesteA { //... }
public sealed class TesteB : TesteA { //... }

In this case above, TesteB inherits from TesteA , but an attempt to inherit from TesteB will cause a compilation error.

The sealed keyword can also be used in methods or properties:

public class TesteA { 
    protected virtual void Hello() { Console.WriteLine("TesteA cumprimenta você!"); }
}
public class TesteB : TesteA { 
    sealed protected override void Hello() { Console.WriteLine("TesteB cumprimenta você!"); }
}

Here it will trigger compilation error CS0239, it is not possible to overwrite a member declared as sealed

public class TesteC : TesteB { 
    protected override void Hello() { Console.WriteLine("TesteC cumprimenta você!"); }
}
    
14.06.2018 / 20:04
2

This is a modifier that prevents other classes from inheriting from this class. This modifier can also be used in properties or methods.

A basic example that exemplifies this:

class B {}
sealed class A: B {}

This inheritance works where A inherits from B , but if it does not, this does not work by restricting the modifier.

Reference sealed (C # Reference)

    
14.06.2018 / 20:03