28

Is it always necessary to follow the sealed keyword with override in the signature of a method like the below code:

public sealed override string Method1(){.....}

I mean, if I want to "seal" the method within the base class without overriding, is the override keyword still necessary?

Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97

3 Answers3

49

Sealing a method only makes sense if you override it.

What happens here is the following:
You are overriding a method from a base class (override) and tell the compiler that classes derived from your class are no longer allowed to override this method (sealed).

If the method is a new one declared by you in your class and you want to prevent derived classes from overriding it, simply don't declare it as virtual.

If the method is declared in a base class but is not overridable sealing it wouldn't make any sense, because it already can't be overriden.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • I didn't mean that. I want to know if say Myclass does not inherit from any class and has a method which I want to be sealed, this method was not overriden in MyClass, but declared for the very first time. – Victor Mukherjee Dec 13 '12 at 11:03
  • 3
    @VictorMukherjee: If you are the one declaring the method you already have full control. If you don't want it to be overriden in derived classes, simply don't make it virtual. See also the second to last paragraph in my answer. – Daniel Hilgarth Dec 13 '12 at 11:05
  • ok, got it. Sorry, forgot about this virtual thing, since I know java, experimenting with c# for just a couple of days. – Victor Mukherjee Dec 13 '12 at 11:05
  • 2
    @VictorMukherjee: Yes, that's an important difference: In Java, methods are per default virtual and you need to explicitly declare them as non-virtual via the `final` keyword. In C# it is the other way around: Per default, methods are non-virtual and you have to explicitly declare them as virtual via the `virtual` keyword. – Daniel Hilgarth Dec 13 '12 at 11:06
  • There are also the `abstract` methods that are like virtual methods but **must** be overridden. – Olivier Jacot-Descombes Mar 17 '18 at 15:37
5

I think Mr. Hilgarth has provided the best answer here , but just to add something new for programmers who have a previous background in Java(like myself), I think most programmers new to C#, tend to confuse sealed with final in Java with respect to overriding.

In Java, the default behaviour without specifying "any" modifier is that the method can be overriden in its derived classes.

While in C#, the default behaviour is that the method cannot be overriden unless explicitly specified using the virtual keyword.

Hope this helps to supplement the best answer above.

Community
  • 1
  • 1
Zaid Khan
  • 786
  • 2
  • 11
  • 24
0

Well, it technically is possible .... however, the solution is in my option kinda dirty.

Imagine having a class A (either in your code base or an external library):

public class A
{
    public virtual void M () { /* implementation */ }
}

You could define an (abstract) class B : A as follows:

public class B : A
{
    public sealed override void M() => base.M();
}

Any class C : B would not be able to override A.M as you have sealed the method (even though you made no semantic changes).

unknown6656
  • 2,765
  • 2
  • 36
  • 52