Reputation: 2269
I want to make an abstract class that imposes a restriction that its child classes must implement an interface. I want to avoid having to implement the interface class in the abstract class. The code below won't do what I'd like. Does anyone have a suggestion of what I could do?
public interface IItem()
{
bool IsUsable();
}
public abstract class Item : IItem
{
MemberVar var;
public void DoSomething()
{
//Do something
}
}
public class Something : Item
{
public bool IsUsable()
{
return true;
}
}
Upvotes: 2
Views: 154
Reputation: 16361
Just make the method abstract
in your abstract class :
public abstract class Item : IItem
{
//...
public abstract bool IsUsable();
}
In the classes that inherit the Item
class use the override
keyword :
public override bool IsUsable()
{
// Do stuff
}
The overriding implementations stubs can be automatically added by VS by right-clicking on the parent abstract class and selecting Implement Abstract Class
:
Upvotes: 12
Reputation: 5691
Simply add abstract
keyword to every method/properties that the interface requires to implement.
For instance, this is how the DoSomething()
method should look:
public abstract void DoSomething();
This way the derived classes will have to implement the interface themselves.
Upvotes: -1
Reputation: 16563
implement the interface in the base class with abstract methods and/or properties
Upvotes: 0