Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot be sealed because it's not an override

I've the following class:

namespace Warnings
{
    public abstract class BaseWarningIntField : IWarningInnerDataField
    {
        public string PropName;

        public string HeaderCaption;

        public sealed WarningInnerDataType DataType
        {
            get { return WarningInnerDataType.Integer; }
        }
    }
}

I want the last property DataType to be not overridable, since that's the base class for a warning-detail field of type Integer, so it needs to always return the correct type WarningInnerDataType.Integer.

Anyway, the compiler give me the following error:

'Warnings.BaseWarningIntField.DataType' cannot be sealed because it is not an override

But, as far as I know the override does exactly the opposite of what I'm trying to achieve.

like image 958
Teejay Avatar asked Mar 27 '13 15:03

Teejay


People also ask

What is sealed override?

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 ).

What is the difference between sealed override and abstract override?

The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

Can abstract class have sealed method?

When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.


1 Answers

in C# all methods by default are non-virtual. You can't override non-virtual method in sub-classes. So leaving property as usual will safe you from subclass overriding it.

Sealed is a keyword used in class declaration for inheritance restrictions or is used to stop virtual chain of members of a class hierarchy. But again - this relates to virtual methods and properties.

Trying to override "normal" property in sub-class will result in compile error

'WarningIntField.DataType.get': cannot override inherited member 'BaseWarningIntField.DataType.get' because it is not marked virtual, abstract, or override

To answer you comment, I'll present some code examples to illustrate my point. You can't actually restrict derived classes from hiding a method or property. So next situation is legal and there is no way to overcome it (this related to virtual method and methods denoted with new keyword as well)

class BaseClass
{
    public string Property {get; set;}
}

class DerivedClass : BaseClass
{
    //compiler will give you a hint here, that you are hiding a base class prop
    public string Property {get; set;}
}

The same way you can't restrict of hiding a field in a class by local variable, so this situation is also valid. Note that compiler will also help you to note, that you are hiding class field in by a local variable. This also related to readonly const and simple static fields as well.

int field = 0; //class field
void Foo()
{
    int field = 0; //local variable
}
like image 81
Ilya Ivanov Avatar answered Oct 05 '22 21:10

Ilya Ivanov