Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sealing an interface after implementing it

I am working on a small project and I came across that problem.

The project output is a library containing an interface. I would like to implement that interface and seal the functions in it like this if possible:

public interface ITest
{
    void SomeMethod();
}

class A : ITest
{
    public sealed override SomeMethod()
    {

    }
}

The idea is to have the interface available to everyone and have some specialized class that implements it. The exception is that I want to make sure that if someone create a specialized class of type A, he/she won't be able to change the method's behavior.

The problem is you can't put the "override" keyword in there since the method isn't declared as "virtual" in the interface. And you can't declare it as "virtual" in the interface since it's not allowed. And you can't remove the "override" keyword since it's needed by "sealed".

Any workaround or brainstorming idea would be welcome, but if someone can come up with a solution that includes an interface, I'd be really happy to learn it!

Thanks!

EDIT: Forget this question! Like Ani said, I forgot that by default method in C# are sealed. Seems like it's always good to go back to the basics once in a while...

like image 344
christ.s Avatar asked Mar 22 '11 03:03

christ.s


People also ask

Can an interface be sealed?

Like sealed classes, to seal an interface, add the sealed modifier to its declaration. Then, after any extends clause, add the permits clause, which specifies the classes that can implement the sealed interface and the interfaces that can extend the sealed interface.

Can a sealed class implement interface?

Subclasses of sealed classes must have a proper qualified name. They can't be local nor anonymous objects. enum classes can't extend a sealed class (as well as any other class), but they can implement sealed interfaces.

What is sealed modifier?

sealed (C# Reference) When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A , but no class can inherit from class B .

What does implementing an interface mean?

An Interface is a specification of functionality that a class MUST implement. When you implement an interface, you are specifying to any consumers of your class that you supply the functionality defined in the given Interface.


2 Answers

I may have completely misunderstood the question, but if your intention is to seal the method in A, you can just do:

class A : ITest
{
    public void SomeMethod()  { ... }
}

Unlike Java, methods in C# are sealed by default. Subclasses of A won't be able to override the method since it hasn't been marked virtual.

On the other hand, if your intention is to mark the method 'almost sealed' in the interface, so that it forces upon an implementing class to immediately seal it, that isn't possible. It isn't (and shouldn't be) the business of the interface to dictate such details of implementation - an interface is meant to represent a specification.

like image 125
Ani Avatar answered Sep 21 '22 01:09

Ani


Use an abstract base class with internal visibility. This base class is not visible outside of the library but allows you to seal the method and the class still implements the interface.

public interface ITest
{
    void SomeMethod();
}

internal abstract class SuperA : ITest
{

    public abstract void SomeMethod();

}

class A : SuperA
{
    public sealed override void SomeMethod()
    {

    }
}
like image 33
Martin Doms Avatar answered Sep 23 '22 01:09

Martin Doms