Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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()
    {

    }
}

Your understanding of sealed keyword is incorrect. As a method modifier, sealed is used to prevent a virtual method(defined in the base class) to be override in the next generation of derived classes. For example:

class Base
{
   public virtual void M() { }
}

class Derived : Base
{
   public sealed override void M() { }
}

class A : Derived
{
   public override void M() { } //compile error, M is sealed in Derived
}

Developers can always use new modifier to define a method with the same name in the derived class, that hides the one defined in the base class.

if someone create a specialized class of type A, he/she won't be able to change the method's behavior.

If "specialized class" means a class derived from A, the answer is: he can always hide the method in A, but he can't change the method's behavior.

Why not use an abstract class like below.

Haven't tested it but this should work?

public abstract class Test
{
    public virtual void SomeMethod() {}
    //OR
    public abstract void SomeMethod();//MSDN says:
    //an abstract method is implicitly virtual
}

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

    }
}

Methods in C# are sealed by default.. Here is a sample

class Program
{
    static void Main(string[] args)
    {

        A obj = new A();
        obj.SomeMethod();
        b ss = new b();
        ss.SomeMethod();
        Console.ReadLine();
    }
}

public interface ITest { void SomeMethod(); }

class A : ITest { public void SomeMethod() {

    Console.WriteLine("SomeMethod Called from Class A object");
} }

class b : A
{
    //public override void SomeMethod()
    //{
    //    Console.WriteLine("Called from Class B Object");
    //}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top