Search code examples
.netinheritanceinterfacevirtualsealed

Why can I seal a class implementing an interface but cant seal a member?


Given this interface

public interface IMyInterface
{
    string Method1();
}

Why is this valid

public sealed class InheretedFromInterfaceSealed: IMyInterface
{
    public string Method1()
    {
        return null;
    }
}

But this isnt

public class InheretedFromInterfaceWithSomeSealed: IMyInterface
{
    public sealed string Method1()
    {
        return null;
    }
}

And yet it is a valid scenario for an abstract class

public abstract class AbstractClass
{
    public abstract string Method1();
}
public class InheretedFromAbstractWithSomeSealed: AbstractClass
{
    public sealed override string Method1()
    {
        return null;
    }
}

Solution

  • Because every method is by default sealed, unless it's virtual, or unless you don't say sealed on something that's already virtual and that you're overriding.