Mark Ursino
Mark Ursino

Reputation: 31435

C# - require an interface on a base class but only the implementations in derived classes

I'm trying to create a base class that provides a good amount of re-usable functionality to many classes that will derive from it. By providing this functionality, I'd like to also require the derived classes to implement certain methods as well, i.e. implement an interface. However, I don't want to explicitly tell the derived classes they need to implement the interface, I want that requirement to be defined in the base class. So basically, once a class inherits from the base, it gets functionality, but also the requirement to implement additional methods itself. Here's what I want, and I'm honestly not sure if this is possible:

public interface IExtraStuff {
  bool test();
}

public class BaseControl : System.Web.UI.UserControl, IExtraStuff {

  public bool foo(){
    return true;
  }

  // I don't actually want to implement the test() method in this
  // class but I want any class that derives from this to implement it.

}

MyUserControl1 : BaseControl {

  // this.foo() can be used here

  // according to IExtraStuff from the BaseControl, I need to implement test() here

}

Basically I don't want to have to define MyUserControl1 as:

MyUserControl1 : BaseControl, IExtraStuff

I want it to automatically require the interface once it inherits the extra functionality from BaseControl.

If this is not possible, let me know and that's fine. I just don't know enough about it. As I have it currently written, it compiles, and I feel like I should get a compile error since test() is not defined in MyUserControl1.

UPDATE

I've modified my base class to be abstract (I had done this prior to posting to SO) but I actually can build the project without implementing the abstract method, that's really why I started to ask this question. The code below builds for me which I'm confused about:

public interface IExtraStuff {
  bool test();
}

public abstract class BaseControl : System.Web.UI.UserControl, IExtraStuff {

  public abstract bool test();

  public bool foo(){
    return true;
  }

}

MyUserControl1 : BaseControl {

  // this.foo() can be used here

  // I can build without implementing test() here!

}

UPDATE 2: problem solved

It turns out I had an issue in my solution build setup and the project does not build unless I implement the abstract method from the base (now). This was error on my part in Visual Studio, not an error in the class architecture. Thanks for all the help everyone!

Upvotes: 2

Views: 1785

Answers (3)

Chris Baxter
Chris Baxter

Reputation: 16353

Use abstract methods?

public abstract class BaseControl : System.Web.UI.UserControl, IExtraStuff { 

  public bool foo(){ 
    return true; 
  } 

  public abstract bool test();  
} 

See for more info: http://msdn.microsoft.com/en-us/library/aa664435(VS.71).aspx

Edit Adding dervied class impl.

public class MyCustomControl : BaseControl { 

  public override bool test()
  {
    //Add Code...
  }  
} 

Upvotes: 4

Jesper Larsen-Ledet
Jesper Larsen-Ledet

Reputation: 6723

That's what abstract classes and methods are for

public abstract class BaseControl : IExtraStuff
{
   public abstract bool test();
}

Note, the class needs to be marked as abstract also

Upvotes: 2

Joachim VR
Joachim VR

Reputation: 2340

If you make BaseControl an abstract class, you can omit the interface members, and so, force the implementation in child classes.

Upvotes: 1

Related Questions