35

I have a nice interface, and I want to implement one member of it in a base class so the clients can derive from the base class and have less boiler-plate to write. However, even though declared abstract, the compiler complains that the class does not implement the interface?!? How is this supposed to be done?

Code in question:

public interface ITaskDefinition
{
    ITask CreateTask(TaskId id);
    string Name { get; }
    bool HasName { get; }
}

public abstract class TaskDefinitionBase : ITaskDefinition
{
    private string name_ = null;

    public void SetName(string name)
    {
        name_ = name;
    }

    public string Name
    {
        get
        {
            return name_;
        }
    }

    public bool HasName
    {
        get
        {
            return name_ != null;
        }
    }
}

and the error I get is:

ITaskDefinition.cs(15,27): error CS0535: 'NetWork.Task.TaskDefinitionBase' does not implement interface member 'NetWork.Task.ITaskDefinition.CreateTask(NetWork.Task.TaskId)'
1
  • 1
    As a tip - clicking on the ITaskDefinition in the abstract class and pressing CTRL + . will fix this for you. Commented Nov 9, 2009 at 12:50

3 Answers 3

48

You must add an abstract stub of the method:

public abstract ITask CreateTask(TaskId id);

Inheritors can then override it to implement the interface.

Sign up to request clarification or add additional context in comments.

5 Comments

That's it, I have to explicitly use override in the implementation :/ Thanks!
they really need to add a compiler warning specifically for this case
@Simon_Weaver They already made it an error. What more do you want?
@R.MartinhoFernandes I think he's saying there should be an error/warning that says, 'Abstract type Type' does not implement 'Interface'; create an abstract method stub to solve this error
FYI: Visual Studio 2022 in this case shows a flyout with options "Implement interface" and "Implement interface abstracly" - which is kind of a hint
4

When an object implements an interface it must obey all the constraints applied by the interface. This means that you must implement all properties and methods declared in the interface.

In your case you're missing the implementation of CreateTask. Also, you should note that C# use properties as opposed to Java for example that uses getters/setters. For this reason you don't need a SetName method.

You code should look something like this:

public abstract class TaskDefinitionBase : ITaskDefinition
{
    private string name_ = null;

    public abstract ITask CreateTask(TaskId id);

    public string Name
    {
        get
        {
            return name_;
        }
        set
        {
            name_ = value
        }
    }

    public bool HasName
    {
        get
        {
            return name_ != null;
        }
    }
}

Comments

1

TaskDefinitionBase needs to include CreateTask - if you don't want to implement it, just mark the method as abstract to force any implementing classes to implement it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.