Alex Rivera | Logout

Abstract class does not implement interface

Asked 2010-07-11T15:26:10.010
11

I have an interface so class writers are forced to implement certain methods. I also want to allow some default implemented methods, so I create a abstract class. The problem is that all classes inherit from the base class so I have some helper functions in there.

I tried to write : IClass in with the abstract base, but I got an error that the base didn't implement the interface. Well of course because I want this abstract and to have the users implement those methods. As a return object if I use base I can't call the interface class methods. If I use the interface I can't access base methods.

How do I make it so I can have these helper classes and force users to implement certain methods?

Edit
Report

1 Answer

16

Make sure methods in the base class have the same name as the interface, and they are public. Also, make them virtual so that subclasses can override them without hiding them.

interface IInterface {
   void Do();
   void Go();
}

abstract class ClassBase : IInterface {

    public virtual void Do() {
         // Default behaviour
    }

    public abstract void Go();  // No default behaviour

}

class ConcreteClass : ClassBase {

    public override void Do() {
         // Specialised behaviour
    }

    public override void Go() {
        // ...
    }

}
answered 2010-07-11T15:31:57.867

Your Answer