Tanya
Tanya

Reputation: 1621

implement Interface in an abstract class

Is it possible to implement the interface in an abstract class and change the implemented interface method into abstract?

interface ITest
{
    string GetData();
    int ProcessData();
}

public abstract class MyAbstract:ITest
{
    public int Process()
    {
        // some code
    }

    public abstract string GetData(); // Change the implemented method into abstract Is it Possible?
}

Upvotes: 4

Views: 1353

Answers (1)

Anthony
Anthony

Reputation: 12407

Yes, you can do it. Simply add the abstract keyword and remove the implementation. There's obviously a pitfall with this. Any class which inherits from your abstract class will have to implement GetData themselves now. Depending on how many classes are children of MyAbstract, this may lead to a lot of work and code duplication.

In your case, where the method in question is declared by the interface which MyAbstract implements, you can actually just drop GetData completely and rely on the declaration of this function in ITest.

Upvotes: 1

Related Questions