lintmouse
lintmouse

Reputation: 5119

Public abstract method or interface?

I have an abstract class with a public abstract method on it. I'm wondering if I should just define an interface and have the abstract class implement that instead. Is there a general rule of thumb here? Currently it works just fine, but I want to be mindful of OO conventions. This is currently how it looks:

public abstract class MySuckyClass
{ 
    public bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = this.GetMember(binder.Name);

        if (result == null)
            return false;

        return true;
    }

    public abstract object GetMember(string memberName);
}

Upvotes: 0

Views: 410

Answers (4)

Nigel Findlater
Nigel Findlater

Reputation: 1724

There is a great book called “Framework Design Guidelines” by Cwalina Krzysztof. If I remember rightly one of the guidelines was to use abstract classes if you want the developer to extend your class and use interfaces if you don’t.

Upvotes: 0

paparazzo
paparazzo

Reputation: 45096

This is back from 2003 but it still hold true in my mind

Recommendations for Abstract Classes vs. Interfaces

And you can define constructors in a an abstract class where you cannot in an interface.

Upvotes: 1

mcarpenter
mcarpenter

Reputation: 111

You're asking a question which has a range of different answers. Will probably vary based on opinion.

Personally, I like defining interfaces and then supplying some canned functionality using an abstract class. This allows me to go back to the interface if I need to supply a different implementation, but saves me time when dealing with methods/properties that do the same thing in multiple implementations.

Upvotes: 2

Oded
Oded

Reputation: 499182

The convention is to use an abstraction. In that regard, this means either an interface or an abstract class.

If your code works as it is, don't change it just to add an un-needed interface.

Upvotes: 1

Related Questions