views:

84

answers:

2

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 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 didnt 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 cant call the interface class methods. If i use the interface i cant access base mthods.

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

+4  A: 

Move the interface methods into the abstract class and declare them abstract as well. By this, deriving classes are forced to implement them. If you want default behaviour, use abstract classes, if you want to only have the signature fixed, use an interface. Both concepts don't mix.

Femaref
+10  A: 

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() {
        // ...
    }

}
Mau