7

Can I set rules for the class inheriting my base class. eg. Person : BaseClass, I want Person to implement iSomeKindOfInterface, if Person does not implement the interface, it is not allowed to inherit from BaseClass.

I know this is posibble in generic base classes where you can do the following

public BaseClass<T>
     where T : iSomeKinfOfInterface
Mahmoud Gamal
  • 75,299
  • 16
  • 132
  • 159
Captain0
  • 2,513
  • 2
  • 25
  • 44

2 Answers2

10

You can implement the interface in your base class and force the inheriting class to supply the implementation:

public interface ISomeInterface
{
    void DoSomething();
}

public abstract class BaseClass : ISomeInterface
{
    public abstract void DoSomething();
}

public class Person : BaseClass
{
    public override void DoSomething()
    {
        ...
    }
}
Trevor Pilley
  • 15,778
  • 5
  • 43
  • 59
2

Declare your class as

abstract BaseClass : ISomeKinfOfInterface
Maheep
  • 5,409
  • 2
  • 25
  • 44
  • Can not use abstract class. The classes I am talking about is winforms user controls. When I use an abstract base class, I can not few the user control in the Visual studio designer... – Captain0 Feb 17 '12 at 10:15
  • The error i get in the designer when using generic base class is the same as this http://stackoverflow.com/questions/3933218/form-designer-breaks-on-generic-abstract-usercontrol when using abstract classes the error is similar – Captain0 Feb 17 '12 at 10:24