Stefan Monov
Stefan Monov

Reputation: 11732

My abstract class implements an interface but doesn't implement some of its methods. How do I make it compile?

interface ICanvasTool
{
    void Motion(Point newLocation);
    void Tick();
}

abstract class CanvasTool_BaseDraw : ICanvasTool
{
    protected abstract void PaintAt(Point location);

    public override void Motion(Point newLocation)
    {
        // implementation
    }
}

class CanvasTool_Spray : CanvasTool_BaseDraw
{
    protected abstract void PaintAt(Point location)
    {
        // implementation
    }

    public override void Tick()
    {
        // implementation
    }
}

This doesn't compile. I could add an abstract method "Tick_Implementation" to CanvasTool_BaseDraw, then implement ICanvasTool.Tick in CanvasTool_BaseDraw with a one-liner that just calls Tick_Implementation. Is this the recommended workaround?

Upvotes: 3

Views: 1228

Answers (4)

numerek
numerek

Reputation: 177

An alternative is don't list the interface in the base classes declaration. Each derived class must list ICanvasTool in its declaration if it wants to be implementing the interface and then it is solely responsible for implementing the rest of the interface. One drawback I can see is you can't explicitly implement the interface methods in the base class (ie no ICanvasTool:Motion), but otherwise this is a fairly low overhead version.

public interface ICanvasTool
{
    void Motion(Point newLocation);
    void Tick();
}

public abstract class CanvasTool_BaseDraw
{
    public void Motion(Point newLocation)
    {
        //some implementation
    }
}

public class CanvasTool_Spray : CanvasTool_BaseDraw, ICanvasTool
{
    public void Tick()
    {
        //some implementation
    }
}

Note: I left out PaintAt because it wasn't necessary for the example.

Upvotes: 1

George Handlin
George Handlin

Reputation: 143

As was stated before, an interface is a contract and therefore all of it needs to be implemented. If a a consumer tried to call something that is defined in the interface but not implemented in the concrete class the application would crash.

Upvotes: 0

Krisc
Krisc

Reputation: 1427

You have a few things mixed up..

Motion should be virtual in your base class so that it may be overridden in child classes. Your child class needs to make PaintAt override instead of abstract. The base class needs to implement Tick as an abstract method.

interface ICanvasTool
{
    void Motion(Point newLocation);
    void Tick();
}

abstract class CanvasTool_BaseDraw : ICanvasTool
{
    protected abstract void PaintAt(Point location);

    public virtual void Motion(Point newLocation)
    {
        // implementation
    }

    public abstract void Tick();
}

class CanvasTool_Spray : CanvasTool_BaseDraw
{
    protected override void PaintAt(Point location)
    {
        // implementation
    }

    public override void Tick()
    {
        // implementation
    }
}

Upvotes: 4

Ronald Wildenberg
Ronald Wildenberg

Reputation: 32104

The way to do this is to add an abstract void Tick() method to CanvasTool_BaseDraw and override it in CanvasTool_Spray.

Not every programming language does it this way. In Java you do not have to add an abstract method for every method in the interface(s) you implement. In that case your code would compile.

Upvotes: 6

Related Questions